winform多線程異步更新UI?
首先在窗體上放一個button和一個picturebox
(picturebox visible為false)這個多線程的目的是點擊按鈕后圖片正常的運行,另一線程在睡眠10秒后在執(zhí)行。
private void button1_Click(object sender, EventArgs e)
{
//開啟線程
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(this.Result));
pictureBox1.Visible = true;
button1.Enabled = false;
thread.Start();
}
private void Result()
{
bool ok = this.Do();
this.BeginInvoke(new System.Threading.ThreadStart(delegate()
{
if (ok) MessageBox.Show("成功", "提示");
else MessageBox.Show("失敗", "提示");
pictureBox1.Visible = false;
button1.Enabled = true;
}));
}
private bool Do()
{
System.Threading.Thread.Sleep(10000);
return true;
}