logo

English

이곳의 프로그래밍관련 정보와 소스는 마음대로 활용하셔도 좋습니다. 다만 쓰시기 전에 통보 정도는 해주시는 것이 예의 일것 같습니다. 질문이나 오류 수정은 siseong@gmail.com 으로 주세요. 감사합니다.

[C#] UI Update from Thread, Thread에서 UI 업데이트 하기 샘플 코드

by lizard2019 posted Jan 23, 2019
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
using System;
using System.Windows.Forms;
using System.Threading;
 
namespace UI_Threading_Example
{
    //declare the delegate that we'll use to launch our worker function in a separate thread
    public delegate void workerFunctionDelegate(int totalSeconds);
 
    //declare the delegate that we'll use to call the function that displays text in our text box
    public delegate void poplateTextBoxDelegate(string text);
 
    public partial class Form1 : Form
    {
       public Form1()
       {
          InitializeComponent();
       }
 
       //this function will simply write text to our text box
       //this function will later be called from a worker thread through the use of a delegate using the Invoke method on the form
       void populateTextBox(string text)
       {
          textBox1.Text = textBox1.Text + " " + text;
       }
 
       //this function simulates "work" by simply counting from 1 to totalSeconds
       void workerFunction(int totalSeconds)
       {
          for (int count = 1; count <= totalSeconds ; count++)
          {
             //we use this.Invoke to send information back to our UI thread with a delegate
             //if we were to try to access the text box on the UI thread directly from a different thread, there would be problems
             this.Invoke(new poplateTextBoxDelegate(populateTextBox), new object[] { count.ToString() });
             Thread.Sleep(1000);
          }
       }   
 
       //this function is executed when we click the first button in the windows form
       //this is the PROPER WAY to do work in a UI situation
       //the worker function is launched in a separate thread so that our UI will remain responsive while it does work
       private void buttonNewThread_Click(object sender, EventArgs e)
       {
          workerFunctionDelegate w = workerFunction;
          w.BeginInvoke(15, null, null);
       }
 
       //this function is executed when we click the second button in the windows form
       //it's an example of WHAT NOT TO DO because if we click this button
       //the UI will become completely unresponsive for 15 seconds while the worker fucntion is executed
       private void buttonCurrentThread_Click(object sender, EventArgs e)
       {
          workerFunction(15);
       }
 }
TAG •

List of Articles
No. Subject Author Date Views
45 세마포어의 개념과 사용법 digipine 2017.10.29 765
44 [Windows] DOS 명령어 실행하고 결과 스트링 가져오는 샘플 코드 digipine 2017.11.02 2689
43 [WINCE] 키보드 및 마우스 메시지 후킹하기 digipine 2017.10.29 809
42 [WINCE] 메모리카드 상태 감시 digipine 2017.10.29 327
41 [WINCE] Process, Thread API 함수 사용법 digipine 2017.10.29 960
40 [WINCE] MulDiv 함수 구현 digipine 2017.10.29 699
39 [WINCE] IAT Hooking 방법과 소스 코드 digipine 2017.10.29 659
38 [WIN32] 파일 핸들로 파일 명 구하기 digipine 2017.10.29 1066
37 [WIN32] 실행 중인 프로세스를 외부에서 강제로 종료, 안전한 TerminateProcess digipine 2017.10.29 3460
36 [WIN32] Process ID로 HWND 구하기 digipine 2017.10.29 5019
35 [Win32] HBITMAP Contrast 조절하는 코드 - RGB 이미지 보정 엉뚱도마뱀 2018.05.04 737
34 [WIN32] API Hook 정리 문서 digipine 2017.10.29 1970
33 [WIN32, WINCE] 디스크 용량 구하는 방법 API GetDiskFreeSpaceEx digipine 2017.10.29 1569
32 [Win API]프로세스 아이디와 윈도우 핸들을 이용 파일명 구하기 digipine 2017.10.29 1393
31 [VC++, WInAPI] 폴더를 통채로 지우기, 서브 폴더 포함, DeleteAllFiles digipine 2017.10.29 1763
30 [MFC] Dialog에서 부모 윈도우 알아내기 digipine 2017.10.28 923
29 [DirectShow] 화면 원본 비율유지 digipine 2017.10.29 960
28 [C#] 프로그램 종료 방법 lizard2019 2019.01.23 6789
27 [C#] 코드 실행 시간 측정 및 DateTime 스트링으로 변환 포맷 lizard2019 2019.01.23 23017
26 [C#] StreamReader 에서의 한글 Encoding 문제 digipine 2017.10.29 1000
Board Pagination Prev 1 2 3 Next
/ 3