[C#/WPF] DispatcherObject 클래스 : Dispatcher 속성을 사용해 장기 실행 계산이 포함된 단일 스레드 애플리케이션 만들기
C#/WPF 2023. 1. 7. 23:42728x90
반응형
728x170
■ DispatcherObject 클래스의 Dispatcher 속성을 사용해 장기 실행 계산이 포함된 단일 스레드 애플리케이션 만드는 방법을 보여준다.
▶ MainWindow.xaml
<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
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
}
}
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] Border 클래스 : BorderThickness 속성을 변경시키는 애니메이션 만들기 (0) | 2023.01.09 |
---|---|
[C#/WPF] ControlTemplate 엘리먼트 : 버튼 배경색을 그라데이션 처리한 Button 엘리먼트 정의하기 (0) | 2023.01.08 |
[C#/WPF] Frame 클래스 : Navigated 이벤트를 사용해 스크립트 오류 억제하기 (0) | 2023.01.08 |
[C#/WPF] Dispatcher 클래스 : Run 정적 메소드를 사용해 다중 윈도우 다중 스레드 사용하기 (0) | 2023.01.08 |
[C#/WPF] 백그라운드 스레드로 차단 작업 처리하기 (0) | 2023.01.08 |
[C#/WPF] ObjectCache 클래스 : 응용 프로그램 데이터 캐싱하기 (0) | 2023.01.06 |
[C#/WPF] RenderOptions 클래스 : SetCachingHint 정적 메소드를 사용해 캐싱 힌트 옵션 설정하기 (0) | 2023.01.04 |
[C#/WPF] HwndHost 클래스 : Win32 컨트롤 호스트하기 (0) | 2023.01.04 |
[C#/WPF] WindowsFormsHost 클래스 : PropertyMap 속성을 사용해 WinForm 컨트롤 속성 매핑 설정하기 (0) | 2023.01.02 |
[C#/WPF] WindowsFormsHost 엘리먼트 : 하이브리드 애플리케이션에서 데이터 바인딩하기 (0) | 2022.12.31 |
댓글을 달아 주세요