You can use the WaitHandle.WaitOne overload that takes a timespan
indicating the number of milliseconds to wait for completion as a simplified
thread / method execution timeout mechanism.
AutoResetEvent autoEvent = new AutoResetEvent(false);
new WaitCallback(WorkMethod), autoEvent);
// Wait for work
method to signal.
if(autoEvent.WaitOne(new TimeSpan(0, 0, 1), false))
Console.WriteLine("Work method signaled.");
Console.WriteLine("Timed out waiting for work
"method to signal.");
void WorkMethod(object stateInfo)
// Simulate time spent working.
// Signal that
work is finished.
How does it work?
create a new AutoResetEvent to be able to signal our thread, with its initial
state being unsignaled (false)
2) We queue a new WorkItem onto a ThreadPool
Thread, passing in a new WaitCallback pointing to our method that is to do the
work, and passing the AutoReset Event as our State information.
pass the WaitOne method of the AutoResetEvent as a boolean test, using the overloaded
method that takes a TimeSpan timeout in milliseconds. This blocks the current
thread until the WaitHandle is signaled, or times out. If signaled, that means
the test will be true, and we know our work method completed before the timeout.
we know that we have reached the specified timeout before our
work method completed and was able to call the Set method of our AutoResetEvent,
which was cast out of the object stateInfo that was a parameter to our workmethod.
You can even "wrap this all up" into a nice packaged
method in a class if you like, where you would pass in the workmethod, any parameters,
the timeout, and return a boolean to indicate success or timeout.
WaitCallback and Work Method must have the same parameters ( "object"),
so if you need multiple parameters for your Work Method, you can wrap them up
in the StateInfo parameter. For example you could pass an instance of a class
that holds the various parameters and values you need, and in the callback method,
cast the State object back to an instance of your class to retrieve your parameters.
Submission Date: 6/27/2006 5:32:42
Submitted By: Peter Bromberg
My Home Page: http://www.eggheadcafe.com