public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(CreateUI));
t.Start();
}
private void CreateUI()
{
AddTextBox();
}
private void AddTextBox()
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(delegate { AddTextBox(); }));
return;
}
TextBox tb = new TextBox();
tb.Text = "test";
this.Controls.Add(tb);
}
}
可以在delegate函数中传递多参数
本文介绍了一个使用C#实现的UI线程安全示例。通过创建新线程来执行UI更新任务,并利用Invoke方法确保在主线程中安全地进行UI组件的添加操作。该示例展示了如何在多线程环境中正确处理UI元素。
2029

被折叠的 条评论
为什么被折叠?



