Skip to content

Instantly share code, notes, and snippets.

@trampster
Created October 8, 2012 08:41
Show Gist options
  • Save trampster/3851445 to your computer and use it in GitHub Desktop.
Save trampster/3851445 to your computer and use it in GitHub Desktop.
public static class WidgetExtensions
{
private static Thread _uiThread;
/// <summary>
/// Determines whether we need to invoke to get onto the UI thread
/// </summary>
public static bool InvokeRequired(this Gtk.Widget widget)
{
if(_uiThread == null)
{
Application.Invoke((_,__) =>
{
_uiThread = Thread.CurrentThread;
});
return true;
}
if(Thread.CurrentThread == _uiThread)
{
return false;
}
return true;
}
/// <summary>
/// Runs the action on the UI thread,
/// Optimized to only invoke if we are not already on the UI thread.
/// </summary>
public static void Invoke(this Gtk.Widget widget, System.Action action)
{
if(widget.InvokeRequired())
{
Application.Invoke((_,__) =>
{
action();
});
return;
}
action();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment