怎样通过一个子线程修改一个WINDOWS控件的值

2025-04-28 00:16:52
推荐回答(1个)
回答1:

public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Thread th = new Thread(new ThreadStart(infoss));
th.IsBackground = true;
th.Start();

}
private void infoss()
{
while (true)
{
SetTime();
Thread.Sleep(1000);
}
}

public delegate void SetTimeInfo(string timeinfo);//委托
private void SetTime()
{
SetTimeInfo info = new SetTimeInfo(SetTextTime);
this.BeginInvoke(info, DateTime.Now.ToString());//异步执行
}

private void SetTextTime(string timeinfo)
{
if (this.textBox1.InvokeRequired)
{
SetTimeInfo time = new SetTimeInfo(SetTextTime);
this.textBox1.BeginInvoke(time, timeinfo);
}
else
{
this.textBox1.Text = timeinfo;
}
}