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 C# - 한글로된 폰트명 처리 방법 개선 (Font Name Localization) digipine 2017.11.02 1393
44 C# 으로 구현한 화면 캡춰 클래스 1 digipine 2017.11.02 34021
43 CreateSemaphore Semaphore Manager digipine 2017.10.29 447
42 Customizing GINA, Part 1 digipine 2017.10.28 21759
41 Customizing GINA, Part 2 digipine 2017.10.28 96759
40 DLL과 EXE간의 데이타 공유하기 digipine 2017.10.29 837
39 GINA(Graphical Identification aNd Authentication), SAS(Secure Attention Sequence) digipine 2017.10.29 1261
38 MS의 Hot Fix API의 유형 연구 digipine 2017.10.29 312
37 Mutex, Critical Section Class 만들기 digipine 2017.10.29 392
36 RegEnumKeyEx 함수 사용법 digipine 2017.10.29 756
35 RPC에 대하여... (1) : RPC 가 사용하는 TCP/IP 포트는 ? digipine 2017.10.29 1296
34 RPC에 대하여... (2) : RPC 가 사용하는 포트를 바꿔보자 digipine 2017.10.29 1118
33 RPC에 대하여... (3) : RPC 작동을 위한 테스트 방법 digipine 2017.10.29 543
32 Serialize를 이용한 객체 복사하기 (Copy constructor) digipine 2017.10.29 499
31 The .Net Developer's Guide to Directory Services Programming digipine 2017.10.29 7239
30 VC++ UTF8 변환 관련 매크로 digipine 2017.11.02 8779
29 VC++ 에서 대소문자 변경하는 함수 digipine 2017.10.29 311
28 VC++(MFC)에서 MDB 생성 / 압축 / 연동관리자 digipine 2017.10.29 2658
27 VS2003 이상에서 iostream 구현의 문제점 digipine 2017.10.29 256
26 VS2005 ConvertBSTRToString 에서 LNK2019 에러 대처법 digipine 2017.10.29 192
Board Pagination Prev 1 2 3 Next
/ 3