I have a slow method, while executing it on timer (System.Timers.Timer). It sometimes gets longer to execute than the timer’s timeout, which causes the timer to wait. I want while I set the timer for 30ms for example, after 30ms, to reset the method, not wait. What kind of timer should I use?
EDIT:
void OnTimerElapsed() {
SomeMethod(args1);
SomeMethod(args2);
SomeMethod(args3);
}
SomeMethod is located in another assembly. It’s a sync method which requests data from another application. I don’t know why it sometimes hangs. That causes the timer to pause until SomeMethod() continues.
Use the
System.Timers.Timerand set the itsAutoResetto false, and only start it when the previous elapsed finished or at your custom condition. Here is the pattern that I use:Edit: If you like to watch the timer and if the operation takes longer time then you should not use timer in the first place, instead use wrap your operation in a new thread and use
MaunalResetEvent, use itsWaitOnemethod to specify the timeout, if the timeout occurs then stop or about the operation.