■ Dispather 클래스의 BeginInvoke 메소드를 사용해 크로스 스레드(Cross-Thread)를 처리하는 방법을 보여준다.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
using System; using System.Threading; using System.Windows.Controls; using System.Windows.Threading; ... private TextBlock textBlock; ... ThreadStart threadStart = delegate() { for(int i = 0; i < 10; i++) { Thread.Sleep(100); DispatcherOperation dispatcherOperation = Dispatcher.BeginInvoke ( DispatcherPriority.Normal, new Action<string, int>(SetMessage), "테스트", i ); DispatcherOperationStatus dispatcherOperationStatus = dispatcherOperation.Status; while(dispatcherOperationStatus != DispatcherOperationStatus.Completed) { dispatcherOperationStatus = dispatcherOperation.Wait(TimeSpan.FromMilliseconds(1000)); if(dispatcherOperationStatus == DispatcherOperationStatus.Aborted) { // Abort시 처리한다. } } } }; new Thread(this.threadStart).Start(); ... #region 메시지 설정하기 - SetMessage(message, value) /// <summary> /// 메시지 설정하기 /// </summary> /// <param name="message">메시지</param> /// <param name="value">값</param> private void SetMessage(string message, int value) { this.textBlock.Text = string.Format("{0} : {1}", message, value); } #endregion |