using System;
using System.Threading;
using System.Windows.Forms;
class Program
{
static TextBox textBox;
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// 创建主窗体和 TextBox 控件
Form form = new Form();
textBox = new TextBox();
form.Controls.Add(textBox);
// 启动一个新线程来更改 TextBox 的值
Thread thread = new Thread(ChangeTextBoxText);
thread.Start();
// 运行应用程序主消息循环
Application.Run(form);
}
// 模拟工作线程
static void ChangeTextBoxText()
{
// 使用委托将更改操作切换到 UI 线程执行
textBox.Invoke(new Action(() =>
{
textBox.Text = "New Value";
}));
}
}
Winform跨线程访问主线程中的内容
最新推荐文章于 2024-04-26 15:30:27 发布