.Net/Winform
C# 크로스쓰레드 문제 깔끔하게 해결하기
동구밖과수원
2013. 1. 9. 17:27
익명 메서드를 통한 깔끔한 코딩~
Winform
/// <summary> /// 관망 해석 중 Percent가 변경되면 발생합니다. /// </summary> /// <param name="value"></param> void global_PecentageChangedEvent(int value) { this.Invoke(new MethodInvoker( delegate() { this.ts_proBar.Value = value; } ) ); } /// <summary> /// 관망 해석 시작 전 Percent 최소 최대값을 Callback 받습니다. /// </summary> /// <param name="min"></param> /// <param name="max"></param> void global_InitProgressBarCallback(int min, int max) { this.Invoke(new MethodInvoker( delegate() { this.ts_proBar.Minimum = min; this.ts_proBar.Maximum = max; } ) ); }
WPF
private void btnTest1_Click(object sender, RoutedEventArgs e) { Thread thread = new Thread(UpdateButton); thread.Start(); } public void UpdateButton() { if (this.btnTest2.Dispatcher.CheckAccess()) { this.btnTest2.Content = "hi"; } else { this.btnTest2.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (Action)delegate() { UpdateButton(); }); } }