I have requirement to cancel method execution if it takes the more than two seconds to complete and restart it on another thread.
So, is there any way/call back mechanis
It would be good if you can find it. I've been looking for it too.
What I usually do is start the method in another Thread
, and start a Timer
with 2 seconds in this case. The first time it raises the event, just do:
if (a.IsAlive)
{
a.Abort();
}
Two important things:
Thread
declared should be visible by the method that handles the timerAbort()
, it raises ThreadAbortException, so you should correctly handle it in the method.