■ SynchronizationContext 클래스 : 크로스 스레드(Cross Thread) 처리하기
------------------------------------------------------------------------------------------------------------------------
▶ MainForm.cs
using System; using System.Threading; using System.Windows.Forms;
namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary> /// 동기화 컨텍스트 /// </summary> private SynchronizationContext context;
/// <summary> /// 실행 여부 /// </summary> private bool isRunning;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent();
this.context = SynchronizationContext.Current;
this.isRunning = false;
this.runButton.Click += runButton_Click; }
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event
#region 실행 버튼 클릭시 처리하기 - runButton_Click(sender, e)
/// <summary> /// 실행 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void runButton_Click(object sender, EventArgs e) { if(this.isRunning) { return; }
this.runButton.Enabled = false;
this.isRunning = true;
Thread thread = new Thread(ProcessThread);
thread.Start(); }
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 스레드 처리하기 - ProcessThread()
/// <summary> /// 스레드 처리하기 /// </summary> private void ProcessThread() { this.context.Send ( _ => {
this.messageLabel.Text = "처리를 시작합니다.";
this.progressBar.Value = 50; }, null );
Thread.Sleep(3000);
this.context.Send ( _ => { this.progressBar.Value = 100;
this.messageLabel.Text = "처리가 완료되었습니다.";
this.isRunning = false;
this.runButton.Enabled = true; }, null ); }
#endregion } }
|
------------------------------------------------------------------------------------------------------------------------
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] 투명 배경 스플래시 이미지 사용하기 (0) | 2019.08.15 |
---|---|
[C#/WINFORM] InstalledFontCollection 클래스 : 영문 폰트명 리스트 구하기 (0) | 2019.08.02 |
[C#/WINFORM] 타원과 원 내부 마우스 위치 여부 구하기 (0) | 2019.07.14 |
[C#/WINFORM] 오각형(Pentagram) 그리기 (0) | 2019.07.14 |
[C#/WINFORM] Parallel 클래스 : For 정적 메소드를 사용해 비트맵 처리하기 (0) | 2019.07.13 |
[C#/WINFORM] SynchronizationContext 클래스 : 크로스 스레드(Cross Thread) 처리하기 (0) | 2019.07.13 |
[C#/WINFORM] 실시간 스트리밍 프로토콜(RTSP)을 사용해 동영상 재생하기 (0) | 2019.06.14 |
[C#/WINFORM] 웹 카메라 사용하기 (0) | 2019.06.12 |
[C#/WINFORM] A* (AStar) 길 찾기 알고리즘 사용하기 (0) | 2019.06.09 |
[C#/WINFORM] Screen 클래스 : AllScreens 정적 속성을 사용해 다른 모니터에서 폼 표시하기 (0) | 2019.05.25 |
[C#/WINFORM] ParentControlDesigner 클래스를 사용해 디자이너 모드에서 편집 가능한 사용자 컨트롤 만들기 (0) | 2019.05.08 |
댓글을 달아 주세요