我想创建一个每1秒工作一次的线程,当它完成时(当我变为真)时,它将为另一个对象发送事件。
fun(run on another thread)
{
while (true)
{
if(cond==true)
{
send event
break
finish the thread
}
else
sleep(1000)(and to the function again
}
}
当另一个对象在事件中返回true并且线程完成
时会执行某些操作我认为线程池在这里最好 但是我在c#上编写简单的代码来实现它并不成功
答案 0 :(得分:0)
在这种情况下你没有说过任何否定计时器使用的东西 - 所以我会用一个。
//Declare a timer (possibly global in this instance)
Timer _timer;
//Put this where you want to start checking. (possibly in main thread)
Timer _timer = new System.Timers.Timer(1000);
//Check every second and fire the timer_Elapsed event
_timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
//Start the timer
_timer.Enabled = true;
//Stop overlapping timers (we will start the timer after it completes it's check)
_timer.AutoReset = false;
//Seperate timer event method.
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
//Check your condition here and send off event if true. this will call
//this method again and again every second.
_timer.Enabled = true;
}