Skip to content

Instantly share code, notes, and snippets.

@EifelMono
Last active August 29, 2015 14:04
Show Gist options
  • Save EifelMono/0bec6f5143efbabcf68c to your computer and use it in GitHub Desktop.
Save EifelMono/0bec6f5143efbabcf68c to your computer and use it in GitHub Desktop.
InvokeOnMainThread for WPF and Forms
// 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