首先看一下一个例子:
AutoResetEvent auto = new AutoResetEvent(false);
private void button2_Click(object sender, EventArgs e)
{
this.label1.Text = "点击了btn2";
//Thread.Sleep(2000);
auto.WaitOne(2000);
}
如果运行上面的代码
1、使用Sleep的时候,会出现停顿2s后才会刷新界面
2、使用WaitOne的时候,会立刻刷新出修改的字符串。
综合所述:
从Msdn上找了相应函数的说明
Thread.Sleep();//Suspends the current thread for the specified number of milliseconds.
WaitOne();//Blocks the current thread until the current WaitHandle receives a signal,
看用词上的区别,一个是挂起,一个是阻塞。
不过WaitOne();的时候,确定是没有人发送信号的。Set是没有调用的。
另外一个很隐藏的问题,就是如果使用sleep在主程序中使用了死循环,会把自定义的消息堆栈阻塞,其他线程有win32消息发出的时候,会被阻塞。