110,539
社区成员
发帖
与我相关
我的任务
分享
if(!execWaitHandle.WaitOne(timeout))//if wait timeout and return false
{
stopWaitHandle.Set();
if (execWaitHandle.WaitOne())//make sure stop success
{
//todo
}
}
static void Main(string[] args)
{
var execWaitHandle = new AutoResetEvent(false);
var stopWaitHandle = new AutoResetEvent(false);
Task.Factory.StartNew(() => { DoIt(stopWaitHandle, execWaitHandle); });
const int timeout = 10000;
if(!execWaitHandle.WaitOne(timeout))//if wait timeout and return false
{
if(stopWaitHandle.WaitOne())//make sure stop success
{
//todo
}
}
}
static void DoIt(AutoResetEvent stopWaitHandle, AutoResetEvent execWaitHandle)
{
while(true)
{
//todo, write your business logic here
if(stopWaitHandle.WaitOne(0)) //if not set, continue your business logic
{
break;
}
}
execWaitHandle.Set();
}