Skip to content

Instantly share code, notes, and snippets.

@kmorcinek
Created May 1, 2018 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmorcinek/7b52ba4dd73faa26790ff45f5b51ee69 to your computer and use it in GitHub Desktop.
Save kmorcinek/7b52ba4dd73faa26790ff45f5b51ee69 to your computer and use it in GitHub Desktop.
Solve a cross-threading Exception in WinForms
using System;
using System.ComponentModel;
public static class ISynchronizeInvokeExtensions
{
public static void InvokeOnUiThread<T>(this T @this, Action<T> action) where T : ISynchronizeInvoke
{
if (@this.InvokeRequired)
{
@this.Invoke(action, new object[] { @this });
}
else
{
action(@this);
}
}
public static void InvokeOnUiThread<T>(this T @this, Action action) where T : ISynchronizeInvoke
{
if (@this.InvokeRequired)
{
@this.Invoke(action, new object[] { @this });
}
else
{
action();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment