Created
April 18, 2010 06:31
-
-
Save karno/370044 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Windows.Forms; | |
namespace Std | |
{ | |
public static class Threading | |
{ | |
/// <summary> | |
/// Invoking method with thread safe. | |
/// </summary> | |
/// <param name="ctrl">Control</param> | |
/// <param name="delg">Delegate</param> | |
/// <param name="args">Arguments</param> | |
/// <returns>object</returns> | |
public static object SafeOperate(this Control ctrl, Delegate delg, params object[] args) | |
{ | |
if (ctrl == null || delg == null || ctrl.IsDisposed || !ctrl.IsHandleCreated) | |
return null; | |
try | |
{ | |
if (ctrl.InvokeRequired) | |
return ctrl.Invoke(delg, args); | |
else | |
return delg.DynamicInvoke(args); | |
} | |
catch (InvalidOperationException) { return null; } | |
} | |
/// <summary> | |
/// Invoking method thread safely. | |
/// </summary> | |
/// <param name="ctrl">Control</param> | |
/// <param name="delg">Action delegate</param> | |
/// <returns>object</returns> | |
public static object SafeOperate(this Control ctrl, Action delg) | |
{ | |
return SafeOperate(ctrl, delg, null); | |
} | |
/// <summary> | |
/// Invoking method thread safely. | |
/// </summary> | |
/// <param name="ctrl">Form</param> | |
/// <param name="delg">Action<T> delegate</param> | |
/// <param name="args">Arguments</param> | |
/// <returns>object</returns> | |
public static object SafeOperate<T>(this Control ctrl, Action<T> delg, T args) | |
{ | |
if (ctrl == null || delg == null || ctrl.IsDisposed || !ctrl.IsHandleCreated) | |
return null; | |
if (ctrl.IsDisposed || !ctrl.IsHandleCreated) | |
return null; | |
if (ctrl.InvokeRequired) | |
return ctrl.Invoke(delg, args); | |
else | |
return delg.DynamicInvoke(args); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment