Add a SynchronizationContext implementation for GLib (bnc#621444)

Create a GLibSynchronizationContext that sends code to be run on the
GLib main loop, and set it as the current SynchronizationContext in
Gtk.Init().

When you use the await keyword to do a task asynchronously, by default
the awaiter will capture the current SynchronizationContext, and if
there was one, when the task completes it’ll Post the supplied
continuation back to that context, rather than running it on whatever
thread it wants.

This means that if you use the async/await keywords in your Gtk# app,
things will now work as expected with the GTK main thread. For example:

static async void DoWork () // called in the GTK main thread
{
    // Do some stuff with the UI...
    label.Text = "Starting Work";

    // Run something asynchronously, UI is not frozen
    int res = await DoLongOperation ();

    // Do some more UI stuff, it'll run on the GTK main thread
    label.Text = "Work done";
}

Signed-off-by: Bertrand Lorentz <bertrand.lorentz@gmail.com>
This commit is contained in:
Rickard Edström 2012-11-08 22:26:09 +01:00 committed by Bertrand Lorentz
parent 135e4cd258
commit 8e07e7d225
4 changed files with 76 additions and 0 deletions

View file

@ -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;
});
}
}
}

View file

@ -29,6 +29,7 @@ sources = \
GException.cs \ GException.cs \
GInterfaceAdapter.cs \ GInterfaceAdapter.cs \
GInterfaceAttribute.cs \ GInterfaceAttribute.cs \
GLibSynchronizationContext.cs \
Global.cs \ Global.cs \
GString.cs \ GString.cs \
GType.cs \ GType.cs \

View file

@ -85,5 +85,6 @@
<Compile Include="ValueArray.cs" /> <Compile Include="ValueArray.cs" />
<Compile Include="Variant.cs" /> <Compile Include="Variant.cs" />
<Compile Include="VariantType.cs" /> <Compile Include="VariantType.cs" />
<Compile Include="GLibSynchronizationContext.cs" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -23,6 +23,7 @@ namespace Gtk {
using System; using System;
using System.Reflection; using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading;
using Gdk; using Gdk;
public partial class Application { public partial class Application {
@ -73,6 +74,8 @@ namespace Gtk {
int argc = 0; int argc = 0;
gtk_init (ref argc, ref argv); gtk_init (ref argc, ref argv);
SynchronizationContext.SetSynchronizationContext (new GLib.GLibSynchronizationContext ());
} }
static bool do_init (string progname, ref string[] args, bool check) static bool do_init (string progname, ref string[] args, bool check)