[blam] Backport GLibSynchronizationContext from gtk-sharp



commit a61384172528ed8651ff8a2c7c8546dd87bbcccc
Author: Carlos Martín Nieto <cmn dwim me>
Date:   Sat Jun 15 13:43:33 2013 +0200

    Backport GLibSynchronizationContext from gtk-sharp
    
    The unreleased version of gtk-sharp provides this class (and
    automatically set this synchronization context) so C# can use the
    right context to continue executing after 'await'.
    
    Copy this file so we can use it and set the synchronization context
    after initializing the app so await works correctly.

 blam.csproj                       |    1 +
 src/Application.cs                |    2 +-
 src/GLibSynchronizationContext.cs |   71 +++++++++++++++++++++++++++++++++++++
 src/ItemList.cs                   |    7 +---
 4 files changed, 75 insertions(+), 6 deletions(-)
---
diff --git a/blam.csproj b/blam.csproj
index 458d4cc..1f66b3d 100644
--- a/blam.csproj
+++ b/blam.csproj
@@ -109,6 +109,7 @@
     <Compile Include="gtk-gui\generated.cs" />
     <Compile Include="gtk-gui\Imendio.Blam.AddChannelDialog.cs" />
     <Compile Include="src\AddChannelDialog.cs" />
+    <Compile Include="src\GLibSynchronizationContext.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <ItemGroup>
diff --git a/src/Application.cs b/src/Application.cs
index cbe3295..b4b35a2 100644
--- a/src/Application.cs
+++ b/src/Application.cs
@@ -145,7 +145,7 @@ namespace Imendio.Blam {
             SetupDBus();
 
             Gtk.Application.Init ();
-            SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
+            SynchronizationContext.SetSynchronizationContext(new GLib.GLibSynchronizationContext());
 
             Proxy.InitProxy ();
 
diff --git a/src/GLibSynchronizationContext.cs b/src/GLibSynchronizationContext.cs
new file mode 100644
index 0000000..0f89092
--- /dev/null
+++ b/src/GLibSynchronizationContext.cs
@@ -0,0 +1,71 @@
+//
+// GLibSynchronizationContext.cs
+// 
+// Author:
+//       Rickard Edström <ickard gmail com>
+//
+// Copyright (c) 2010 Rickard Edström
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+using System;
+using System.Threading;
+
+namespace GLib
+{
+       public class GLibSynchronizationContext : SynchronizationContext
+       {
+               public override void Post (SendOrPostCallback d, object state)
+               {
+                       PostAction (() => d (state));
+               }
+
+               public override void Send (SendOrPostCallback d, object state)
+               {
+                       var mre = new ManualResetEvent (false);
+                       Exception error = null;
+                       
+                       PostAction (() =>
+                       {
+                               try {
+                                       d (state);
+                               } catch (Exception ex) {
+                                       error = ex;
+                               } finally {
+                                       mre.Set ();
+                               }
+                       });
+                       
+                       mre.WaitOne ();
+                       
+                       if (error != null) {
+                               throw error;
+                       }
+               }
+
+               void PostAction (Action action)
+               {
+                       Idle.Add (() =>
+                       {
+                               action ();
+                               return false;
+                       });
+               }
+       }
+}
diff --git a/src/ItemList.cs b/src/ItemList.cs
index 9acb46e..3a06200 100644
--- a/src/ItemList.cs
+++ b/src/ItemList.cs
@@ -200,11 +200,8 @@ namespace Imendio.Blam {
                        }
 
                        MarkReadCancel = null;
-                       Idle.Add(() => {
-                               item.SetUnread(false);
-                               model.EmitRowChanged(model.GetPath(iter), iter);
-                               return false;
-                       });
+                       item.SetUnread(false);
+                       model.EmitRowChanged(model.GetPath(iter), iter);
                }
 
                private int CompareFunc(TreeModel model, TreeIter a, TreeIter b)


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