在C#的WinForms应用程序中,如果你需要在异步线程中刷新UI,你可以使用Control类的Invoke或BeginInvoke方法。以下是一个示例代码,演示如何在异步线程中刷新UI:
using System;
using System.Threading;
using System.Windows.Forms;
class Program
{
static Form form;
static Label label;
static void Main()
{
form = new Form();
label = new Label();
label.Text = "Hello";
form.Controls.Add(label);
Thread thread = new Thread(UpdateUI);
thread.Start();
Application.Run(form);
}
static void UpdateUI()
{
// 模拟耗时操作
Thread.Sleep(2000);
// 在异步线程中刷新UI
form.Invoke((Action)(() =>
{
label.Text = "Hello, World!";
}));
}
}
在上述代码中,我们在异