From: Tony Johansson on 12 May 2010 18:17 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
From: Tony Johansson on 12 May 2010 19:33 It works now! //Tony "Tony Johansson" <johansson.andersson(a)telia.com> skrev i meddelandet news:O5ZGTDi8KHA.1424(a)TK2MSFTNGP04.phx.gbl... > 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 > >
|
Pages: 1 Prev: plugin object in C# Next: one processor with multiple core and threads |