Created
May 1, 2018 07:14
-
-
Save kmorcinek/7b52ba4dd73faa26790ff45f5b51ee69 to your computer and use it in GitHub Desktop.
Solve a cross-threading Exception in WinForms
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
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