[blam] Use Task and await for RefreshAsync() and RefreshAllAsync()



commit 37d85d36c84ee285ffc2096b1e4fb5207aca1445
Author: Carlos Martín Nieto <cmn dwim me>
Date:   Fri Jun 14 01:59:12 2013 +0200

    Use Task and await for RefreshAsync() and RefreshAllAsync()
    
    This is much nicer to work with and keep us in the right context,
    getting rid of the model and view mismatch.
    
    This needs us to increase the sdk level to 4.5.

 blam.csproj              |    1 +
 src/Application.cs       |    3 ++-
 src/Channel.cs           |   13 ++++---------
 src/ChannelCollection.cs |   40 ++++++++++++++++++----------------------
 src/ChannelGroup.cs      |    9 ++-------
 src/FeedUpdater.cs       |    2 --
 src/Makefile.am          |    2 +-
 7 files changed, 28 insertions(+), 42 deletions(-)
---
diff --git a/blam.csproj b/blam.csproj
index 4fddb30..ccca6e0 100644
--- a/blam.csproj
+++ b/blam.csproj
@@ -11,6 +11,7 @@
     <AssemblyName>blam</AssemblyName>
     <ReleaseVersion>1.9.10</ReleaseVersion>
     <StartupObject>Imendio.Blam.Application</StartupObject>
+    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
     <DebugSymbols>True</DebugSymbols>
diff --git a/src/Application.cs b/src/Application.cs
index d6c5880..0f75b23 100644
--- a/src/Application.cs
+++ b/src/Application.cs
@@ -17,6 +17,7 @@ using System.Collections;
 using System.IO;
 using System.Text;
 using System.Runtime.InteropServices;
+using System.Threading;
 using WebKit;
 #if ENABLE_DBUS
 using DBus;
@@ -649,7 +650,7 @@ namespace Imendio.Blam {
         public void RefreshChannelActivated(IChannel channel)
         {
             if (channel != null) {
-                channel.RefreshAsync(null);
+                channel.RefreshAsync();
             }
         }
 
diff --git a/src/Channel.cs b/src/Channel.cs
index 97b5a9d..fd477ca 100644
--- a/src/Channel.cs
+++ b/src/Channel.cs
@@ -8,6 +8,7 @@ using System.Collections;
 using System;
 using System.Net;
 using System.Xml.Serialization;
+using System.Threading.Tasks;
 using System.ServiceModel.Syndication;
 
 namespace Imendio.Blam {
@@ -26,7 +27,7 @@ namespace Imendio.Blam {
         event ChannelEventHandler Updated;
         void RemoveItems();
                bool Refresh();
-               void RefreshAsync(Action<bool> cb);
+               Task<bool> RefreshAsync();
     }
 
     public class Channel : IChannel {
@@ -301,15 +302,9 @@ namespace Imendio.Blam {
                        return FeedUpdater.Update(this);
                }
 
-               public void RefreshAsync(Action<bool> cb)
+               public Task<bool> RefreshAsync()
                {
-                       var fun = new RefreshDelegate(Refresh);
-                       fun.BeginInvoke(ar => {
-                               bool res = fun.EndInvoke(ar);
-                               if (cb != null)
-                                       cb(res);
-                       },
-                       null);
+                       return Task<bool>.Factory.StartNew(() => Refresh());
                }
        }
 }
diff --git a/src/ChannelCollection.cs b/src/ChannelCollection.cs
index 9a23c02..5709bfa 100644
--- a/src/ChannelCollection.cs
+++ b/src/ChannelCollection.cs
@@ -171,7 +171,7 @@ namespace Imendio.Blam {
            }
        }
 
-        public void Add (IChannel channel)
+        public async void Add (IChannel channel)
         {
             // If we already have this feed, simply return
             if (mChannels.Cast<Channel>().Any(channel.Url.Equals))
@@ -182,25 +182,24 @@ namespace Imendio.Blam {
         }
 
         mChannels.Add (channel);
-            channel.RefreshAsync(updated => {
-                MarkAsDirty(true);
-                if (ChannelAdded != null)
-                    ChannelAdded (channel);
-            });
-       }
+                       bool res = await channel.RefreshAsync();
+                       if (res)
+                               MarkAsDirty(true);
+                       if (ChannelAdded != null)
+                               ChannelAdded (channel);
+               }
 
-        public void Add(ChannelGroup group, IChannel channel)
+        public async void Add(ChannelGroup group, IChannel channel)
         {
             group.Add(channel);
-            channel.RefreshAsync(updated => {
-                channel.Updated += Updated;
+                       bool updated = await channel.RefreshAsync();
+                       channel.Updated += Updated;
 
-                if(ChannelGroupAdded != null){
-                    ChannelGroupAdded(group, channel);
-                }
+                       if(ChannelGroupAdded != null){
+                               ChannelGroupAdded(group, channel);
+                       }
 
-                MarkAsDirty(true);
-            });
+                       MarkAsDirty(true);
         }
 
        public void Updated (IChannel channel)
@@ -238,10 +237,9 @@ namespace Imendio.Blam {
 
         delegate void RefreshAllDelegate();
 
-        public void RefreshAllAsync()
+        public Task RefreshAllAsync()
         {
-            var fun = new RefreshAllDelegate(RefreshAll);
-            fun.BeginInvoke(fun.EndInvoke, null);
+             return Task.Factory.StartNew(RefreshAll);
         }
 
         public void RefreshAll ()
@@ -252,10 +250,8 @@ namespace Imendio.Blam {
             }
 
             Parallel.ForEach(mChannels.Cast<Channel>(), c => {
-                bool updated = c.Refresh();
-                if (updated) {
-                    MarkAsDirty (true);
-                }
+                if (c.Refresh())
+                                       MarkAsDirty(true);
                 new MainloopEmitter (this.ChannelRefreshFinished, c).Emit ();
             });
         }
diff --git a/src/ChannelGroup.cs b/src/ChannelGroup.cs
index 4c1df3d..5f8efb2 100644
--- a/src/ChannelGroup.cs
+++ b/src/ChannelGroup.cs
@@ -167,14 +167,9 @@ namespace Imendio.Blam
                        return true; // Whatever
                }
 
-               public void RefreshAsync(Action<bool> cb)
+               public Task<bool> RefreshAsync()
                {
-                       var fun = new RefreshDelegate(Refresh);
-                       fun.BeginInvoke(ar => {
-                               bool res = fun.EndInvoke(ar);
-                               if (cb != null)
-                                       cb(res);
-                       }, null);
+                       return Task<bool>.Factory.StartNew(Refresh);
                }
     }
 }
diff --git a/src/FeedUpdater.cs b/src/FeedUpdater.cs
index 1c9d6c0..0fb1012 100644
--- a/src/FeedUpdater.cs
+++ b/src/FeedUpdater.cs
@@ -40,8 +40,6 @@ namespace Imendio.Blam {
         public static bool Update(Channel channel)
         {
 
-            bool updated = false;
-
             channel.LastRefreshed = DateTime.Now;
             XmlTextReader reader = null;
             SyndicationFeed feed = null;
diff --git a/src/Makefile.am b/src/Makefile.am
index 9183362..c795d2b 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,6 +1,6 @@
 CSC = $(MCS)
 
-FLAGS = /debug
+FLAGS = /debug -sdk:4.5
 
 RESOURCES = \
            -resource:blam.glade,blam.glade                                  \


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]