类:
public static void Invoke(Control ctl, MethodInvoker method)
{
if (!ctl.IsHandleCreated)
return;
if (ctl.IsDisposed)
return;
if (ctl.InvokeRequired)
{
ctl.Invoke(method);
}
else
{
method();
}
}
调用:
private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(CreateUI));
t.Start();
}
private void CreateUI()
{
AddTextBox("bb");
}
private void AddTextBox(string str)
{
ControlInvoker.Invoke(this, delegate
{
add("测试");
});
}
public void add(string str)
{
TextBox textBox = new TextBox();
textBox.Text =str;
this.Controls.Add(textBox);
}