这是代码,程序已经设置成多线程了。 [MTAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
int i = 0;
delegate void calltest(int value);
private void Form1_Load(object sender, EventArgs e)
{
Thread t = new Thread(new ParameterizedThreadStart(test));
t.Start(this);
}
private void test(object obj)
{
Form1 f = obj as Form1;
while (true)
{
i++;
f.Invoke((calltest)call, new object[] { i });
Thread.Sleep(1000);
}
}
private void call(int value)
{
textBox1.Text = value.ToString();
} private void button1_Click(object sender, EventArgs e)
{
Thread.Sleep(3000);
}
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
int i = 0;
delegate void calltest(int value);
private void Form1_Load(object sender, EventArgs e)
{
Thread t = new Thread(new ParameterizedThreadStart(test));
t.Start(this);
}
private void test(object obj)
{
Form1 f = obj as Form1;
while (true)
{
i++;
f.Invoke((calltest)call, new object[] { i });
Thread.Sleep(1000);
}
}
private void call(int value)
{
textBox1.Text = value.ToString();
} private void button1_Click(object sender, EventArgs e)
{
Thread.Sleep(3000);
}
那么既然UI线程被Sleep了,当然就没法执行Invoke了,不执行Invoke,textBox1的值当然也不更新了。只有当UI线程醒了,才能继续执行Invoke