■ DispatcherObject 클래스의 Dispatcher 속성을 사용해 장기 실행 계산이 포함된 단일 스레드 애플리케이션 만드는 방법을 보여준다.
▶ MainWindow.xaml
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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="TestProject" Width="800" Height="600" FontFamily="나눔고딕코딩" FontSize="16"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <Button Name="startButton" Width="100" Height="30" Content="Start" /> <StackPanel Margin="0 30 0 0" Orientation="Horizontal"> <TextBlock> Biggest Prime Found : </TextBlock> <TextBlock Name="bigPrimeTextBlock" Margin="10 0 0 0"> 3 </TextBlock> </StackPanel> </StackPanel> </Window> |
▶ MainWindow.xaml.cs
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
using System; using System.Diagnostics; using System.Windows; using System.Windows.Threading; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Delegate ////////////////////////////////////////////////////////////////////////////////////////// Public #region 다음 소수 대리자 - NextPrimeDelegate() /// <summary> /// 다음 소수 대리자 /// </summary> public delegate void NextPrimeDelegate(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 계산 계속 여부 /// </summary> private bool continueCalculating; /// <summary> /// 소수가 아닌 경우 /// </summary> private bool notPrime; /// <summary> /// 숫자 /// </summary> private long number = 3; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.startButton.Click += startButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region Start 버튼 클릭시 처리하기 - startButton_Click(sender, e) /// <summary> /// Start 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void startButton_Click(object sender, EventArgs e) { if(this.continueCalculating) { this.continueCalculating = false; this.startButton.Content = "Resume"; } else { this.continueCalculating = true; this.startButton.Content = "Stop"; this.startButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new NextPrimeDelegate(CheckNextNumber)); } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 다음 숫자 체크하기 - CheckNextNumber() /// <summary> /// 다음 숫자 체크하기 /// </summary> public void CheckNextNumber() { this.notPrime = false; for(long i = 3; i <= Math.Sqrt(this.number); i++) { if(this.number % i == 0) { this.notPrime = true; break; } } if(!this.notPrime) { this.bigPrimeTextBlock.Text = this.number.ToString(); } this.number += 2; if(this.continueCalculating) { this.startButton.Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle, new NextPrimeDelegate(CheckNextNumber)); } } #endregion } } |