维修第一次计时器 [英] Servicing the first timer
问题描述
我已经启动了两个threading.timers,它具有这样的回调,在到期时被调用:
I have started two threading.timers which has callback like this called on expiry:
private void N_ArTimeoutExpired(object state)
{
// Timeout raised. Set timeout error state
String message = string.Empty;
message = "N_Ar Timer Expired at -> " + DateTime.Now.ToString("h:mm:ss.fff");
_logger.Info(message);
Iso15765Transport.ErrorEventArgs errorEventArgs = new Iso15765Transport.ErrorEventArgs("N_Ar timer expired");
_owner.SetEvent(errorEventArgs);
}
在到期时,它引发一个事件,该事件调用以下方法:
Here on expiry it raises an event which calls following method:
public void OnTimeOut(object o, ErrorEventArgs e)
{
lock (_locker)
{
string errorMsg = e._errorMsg;
PublishErrorFrame(errorMsg);
ResetAllTimers();
ResetStateMachine();
AbortCurrentReception();
}
}
我的问题是,在这种方法中,我必须维修第一个到期的计时器并拒绝另一个计时器.我该怎么做?
考虑两个计时器同时到期的情况.
我不能放一个标志,只是以这种方式检查.原因是,当第一个计时器到期时,我将重置所有状态机,并重新开始通信.可能是我有一个计时器,在重置状态机之前更早了.
My problem is that in this method i have to service the first timer that expires and reject the other one.How can i do that?
Considering a situation where both timers expires simultaneously.
I cant put a flag and simply check in that way. Reason is that when first timer expires , i reset all state machines and communication is started again. The chances are that i have a timer that expired earlier before resetting state machines.
推荐答案
我认为您正在寻找的是使用 ^ ].
这使您可以在适当的方法中阻止执行,确保您设置的标志一次只能由一个线程设置,从而防止出现竞争状况.
干杯.
I think what you are looking for is the use of Monitor[^].
This allows you to block the execution in the method proper, ensuring that flags you set will be set by one and only one thread at a time and thus, preventing race conditions.
Cheers.
这篇关于维修第一次计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!