Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save posaunehm/3128336 to your computer and use it in GitHub Desktop.
Save posaunehm/3128336 to your computer and use it in GitHub Desktop.
スレッドセーフにメソッドを実行する拡張メソッドスニペット
/// <summary>
/// Controlに対してスレッドセーフにActionを実行する
/// </summary>
/// <param name="control">対象コントロール</param>
/// <param name="action">Controlに対してスレッドセーフに実行したいAction</param>
public static void InvokeThreadSafely(this Control control, Action action)
{
if (control.InvokeRequired)
{
var asyncResult = control.BeginInvoke(action);
//同期っぽい動きにする
control.EndInvoke(asyncResult);
}
else
{
action.Invoke();
}
}
private void UpdateUI()
{
this.InvokeThreadSafely(() =>
{
//TODO: UIの更新コードを記述する
}
);
}
@posaunehm
Copy link
Author

これ合ってるのかな・・・?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment