等待一段时间,直到满足条件 [英] Waiting for some time until a condition is met
问题描述
我有一个这样的代码,它在与主线程不同的线程中执行。
if (_isSTminStarted&&(_stMinExpired == false ))
{
// 等到stMin到期
字符串 message = 字符串跨度> .Empty;
执行
{
message = 等待STmin到期 + DateTime.Now.ToString( h:mm: SS.FFF跨度>);
_logger.Info(message);
} while (_stMinExpired == false );
_isSTminStarted = true ;
_timer.STminTimerStarted(_stMinRecieved);
CanFrame consecutiveFrame = _fragmentedFrameQueue.Dequeue();
PublishFrame(consecutiveFrame);
}
请参阅do while循环,我已添加,因为我必须等到计时器到期并且_stMinExpired设置为true。然后我只发送下一帧。
当System.threading.timer到期时设置此标志。但我不认为这是等到那个时候的理想方式。你可以建议一些方法,我可以在这个线程中等待,直到设置该标志,而不是在循环时放置。
尝试使用 AutoResetEvent [ ^ ]。
正如Firo所提到的,你想要使用其中一个基于事件的等待处理程序(AutoResetEvent或ManualResetEvent)。它们的设计正是你在这里要求的:等到未知时间在另一个线程中发生的事情。
class Test {
AutoResetEvent waitHandle = new AutoResetEvent(false);
void SpawnThread(){
Thread t = new Thread(ThreadMethod);
t.Start();
}
void ThreadMethod(){
waitHandle.WaitOne();
CanFrame consecutiveFrame = _fragmentedFrameQueue.Dequeue();
PublishFrame(consecutiveFrame);
}
void SomeLongOtherMethod(){
DoStuffThatTakesAWhile();
waitHandle.Set(); //这将释放线程。
}
}
你永远不要等待使用紧密循环。这将以100%的CPU运行核心并使您的应用程序不受欢迎。等待句柄和Thread.Sleep也是操作系统级触发器,并将您的线程置于不使用CPU时间的非运行状态。
使用 Thread.Sleep [ ^ ] - 它可以在您的任务执行任何其他操作之前等待特定时间。
I have a code like this which executes in a different thread than main thread.
if(_isSTminStarted && (_stMinExpired == false))
{
//wait till stMin is expired
String message = string.Empty;
do
{
message = "Waiting till STmin expires " + DateTime.Now.ToString("h:mm:ss.fff");
_logger.Info(message);
} while (_stMinExpired == false);
_isSTminStarted = true;
_timer.STminTimerStarted(_stMinRecieved);
CanFrame consecutiveFrame = _fragmentedFrameQueue.Dequeue();
PublishFrame(consecutiveFrame);
}
Please see the do while loop , i have added that because i have to wait until a timer expires and _stMinExpired is set to true. Then only i should send next frame.
This flag is set when a System.threading.timer expires. But i don't think this is the ideal way to wait till that time. can you suggest some ways in which i can wait in this thread till that flag is set rather than putting do while loop.
Try using AutoResetEvent[^].
As Firo mentions, you want to use one of the event based wait handlers (AutoResetEvent or ManualResetEvent) for this. They are designed for exactly what you ask here: waiting until something happens in another thread at an unknown time.
class Test { AutoResetEvent waitHandle = new AutoResetEvent(false); void SpawnThread(){ Thread t = new Thread(ThreadMethod); t.Start(); } void ThreadMethod(){ waitHandle.WaitOne(); CanFrame consecutiveFrame = _fragmentedFrameQueue.Dequeue(); PublishFrame(consecutiveFrame); } void SomeLongOtherMethod(){ DoStuffThatTakesAWhile(); waitHandle.Set(); // This will release the thread. } }
You should never wait by using a tight loop. That will run a core at 100% CPU and make your app unpopular. Wait handles, and Thread.Sleep as well, are OS level triggers and put your thread into a non-running state that doesn't use CPU time.
Use Thread.Sleep[^] instead - it can wait for a specific time before your task does anything else.
这篇关于等待一段时间,直到满足条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!