Last active
August 29, 2015 14:04
-
-
Save EifelMono/0bec6f5143efbabcf68c to your computer and use it in GitHub Desktop.
InvokeOnMainThread for WPF and Forms
This file contains hidden or 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
// For WPF you need the following Assemblies | |
// PresentationCore | |
// PresentationFramework | |
// WindowsBase | |
// WindowsFormsIntegration | |
// For Forms | |
// System.Windows.Forms | |
// create this in the main thread for your Forms application | |
internal static Control MainThreadInvokerControl; | |
protected delegate void TMethod(); | |
public void InvokeOnMainThread(Delegate method) | |
{ | |
if (System.Windows.Application.Current != null) | |
{ | |
try | |
{ | |
System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, method); | |
} | |
catch (Exception ex) | |
{ | |
Log.HandleException(this, ex); | |
} | |
} | |
else | |
MainThreadInvokerControl.Invoke(new TMethod(() => | |
{ | |
try | |
{ | |
method.DynamicInvoke(); | |
} | |
catch (Exception ex) | |
{ | |
Log.HandleException(this, ex); | |
} | |
})); | |
} | |
} | |
// Hosting WPF in Forms | |
// http://www.microsoft.com/germany/msdn/solve/knowhow/howto/cliententwicklung/WieHosteIchWPFSteuerelementeAufWindowsFormsAnwendungen.aspx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment