Created
December 1, 2020 03:00
-
-
Save ryerh/8f6f6cae75d2185a2a275111ae052880 to your computer and use it in GitHub Desktop.
UI Thread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
namespace MultiThreadUI | |
{ | |
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
private void btn_NewThread_Click(object sender, EventArgs e) | |
{ | |
var th = new System.Threading.Thread(CheckStatusInNewThread); | |
th.Name = "StatusCheckThread"; | |
th.Start(); | |
} | |
private void CheckStatusInNewThread() | |
{ | |
// 禁用按钮 | |
this.btn_NewThread.Invoke(new Action(() => | |
{ | |
this.btn_NewThread.Enabled = false; | |
})); | |
// 开始轮询检查 | |
for (int i = 0; i < 5; i++) | |
{ | |
this.textBox1.Invoke(new Action(() => | |
{ | |
this.textBox1.Text = "正在进行第 " + i.ToString() + " 次检查..."; | |
})); | |
// 每次检查 Sleep 1s | |
System.Threading.Thread.Sleep(1000); | |
} | |
// 启用按钮 | |
this.btn_NewThread.Invoke(new Action(() => | |
{ | |
this.btn_NewThread.Enabled = true; | |
})); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment