Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have started two threading.timers which has callback like this called on expiry:
C#
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:
C#
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.
Posted

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900