Created
October 8, 2012 08:41
-
-
Save trampster/3851445 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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