ThreadPool and WaitHandle

T

Tony Johansson

Hi!

In the book I'm reading it says.
In lesson 2, you learned that all kernel-level synchronization
objects(Mutex, Semaphore and Event) use WaitHandle as their base class. The
thread pool also provides a mechanism to use threads in the pool to wait on
these handles and fire off callbacks to methods when the WaitHandle are
signaled. This is done by calling the
ThreadPool.RegisterWaitForSingleObject, as shown in the following example:

So when I run this code I would assume that the callback method
MutexHasFired should have been called no these is no call to that method.
So what is it that I have missed here ??

Mutex mutex = new Mutex(true);
ThreadPool.RegisterWaitForSingleObject(mutex,
new WaitOrTimerCallback(MutexHasFired), null,
Timeout.Infinite, true);

mutex.ReleaseMutex();

The RegisterWaitForSingleObject method takes the WaitHandle object, as well
as a delegete that points to a method that takes an object(thta represents
the thread state specified in the method call), and a Bollean value that
indicates whether the timeout has been reched insted of the WaitHandle being
signaled. the MutesHasFired callback method might look something like this.

static void MutexHasFired(object state, bool timeout)
{
if (timeout)
{
Console.WriteLine("Mutex Time out");
}
else
{
Console.WriteLine("Mutex got signaled");
}
}

//Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top