views:

220

answers:

1

Hi again,

I am looking for a method where I can make sure that no callback is waiting to be executed on a System.Timers.Timer thread after I have called Stop() on it.

Basically I have a timer and a async socket - Either one can invoke an appropriate function when either the time elapses or a new packet is received. Naturally I don't want it to be possible for both things to happen at once.

At the moment I protect the internals of each function via using a mutex:

if (!_mutex.WaitOne(0, true))
{
   return;
}
_TimeOut.Stop();
... // function body goes here
_mutex.ReleaseMutex();

Within the timer there are a number of operations which take place between the time elapsing and the event being fired, thus giving the opportunity that even after stopping the timer, the callback will be executed once the mutex has been released. Meaning the above code does pretty much nothing.

So, is it possible to determine which thread the internals of the timer are running on? and either forcing it out of the callback function, or blocking until that function is complete and letting aboce code take care of things?

Thanks.

A: 

In your callback or event handler, you must always check if the timer is actually enabled before doing anything else.

CannibalSmith
Unfortunately my socket callback restarts the timer - meaning it will be enabled should this case arise.
Courtney de Lautour