正式程序中应该尽量避免使用sleep,至于为啥,请自行百度.
C#: Monitor.Wait的方法和WaitOne方法
Monitor.Wait:
object obj = new object();
// 线程内:
while (true)
{
lock (obj)
{
Monitor.Wait(obj, 1000);// 释放锁,并等待1000毫秒
}
}
WaitOne:
static public AutoResetEvent TaskEvent = new AutoResetEvent(false);
// 线程内
while (true)
{
TaskEvent.WaitOne(1000);
}
C++: WaitForSingleObject
while (true)
{
WaitForSingleObject(m_hExit,1000);// 不激活m_hExit,便会在此等待1000毫秒;
}