Created
July 17, 2012 09:32
-
-
Save posaunehm/3128336 to your computer and use it in GitHub Desktop.
スレッドセーフにメソッドを実行する拡張メソッドスニペット
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
/// <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(); | |
} | |
} |
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
private void UpdateUI() | |
{ | |
this.InvokeThreadSafely(() => | |
{ | |
//TODO: UIの更新コードを記述する | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
これ合ってるのかな・・・?