선생님 안녕하세요!
Winform 위에 Button과 statusStrip를 올린 후에,
statusStrip 안에 있는 ▼ 를 클릭 후, StatusLabel을 눌렀습니다.
namespace Test
{
public partial class Form1 : Form
{
private BackgroundWorker _worker = new BackgroundWorker();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
toolStripStatusLabel1.Text = "";
// ⓐ
TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
Task.Run(() => {
DoSomething();
})
.ContinueWith(task => { toolStripStatusLabel1.Text = "종료"; }, uiScheduler); // ⓑ
}
private void DoSomething()
{
// 4000[ms] = 4초
Thread.Sleep(4000);// 본래는 시간이 소요되는 처리
}
}
}
ⓐ TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
이 코드를 통해 Main Thread(= UI Thread)의 스케줄러를 가져옵니다.
ⓑ .ContinueWith(task => { toolStripStatusLabel1.Text = "종료"; }, uiScheduler);
ContinueWith의 두 번째 매개변수로 Main 스레드(= UI Thread)의 스케줄러를 넘기면
첫 번째 매개변수로 넘어온 람다식을 스케줄러로 넘어온 Main 스레드(= UI Thread)에서 실행하도록 한다.
혹시 위의 코드를 제가 임의로 해석한 부분 중에 혹시 잘못된 것이 있는지 확인해주실 수 있으신가요~?
[최초 등록일: ]
[최종 수정일: 5/5/2021]