by luisabreu via LA.NET [EN] on 5/18/2009 9:13:50 AM
Windows OS offers two kinds of events: auto and manual-reset events. As you might expect by now, any event can be in a signaled or non-signaled state (after all, both of these events are kernel objects). Instead of using signaled and non-signaled terms, you’ll probably hear the words set and reset when talking about these states.
The main difference between auto and manual-reset events relies on the fact that an auto-reset event will automatically reset itself. Notice also that when you use an auto-reset event, only one thread will be awakened (if there are any waiting for the event to be set; if there aren’t, then the auto-reset event object will remain signaled until another thread acquires it).
After reading the previous paragraph, you might think that an auto-reset event exhibits a similar behavior to the one you get when you use a mutex. Even though that is true (you have only one thread accessing the protected resource at a time and only one will be wakened when the event is set), it’s important to understand that events don’t have the concept of ownership. In practice, this means that you wont be able to have recursion (which might be good thing). In practice, this means that you can set the event from any thread (ie, you don’t need to set if from the thread that created the event).
Manual-reset events work in a slightly different way. When a manual-reset event is set, it will remain set until some thread resets it. This means that several waiting threads will be awakened when the object is in the signaled state. Manual-reset events are good for what I call event-based scenarios, where you need to signal several waiting threads that something has happened.
As you can see from the previous model, AutoResetEvent and ManualResetEvent lets you work with events from managed code. You can also see (from the model) that both classes shared the same base class (EventWaitHandle) which isn’t abstract. In fact, if you dig into the code, you’ll notice that AutoResetEvent and ManualResetEvent are there only for passing the correct values to the base EventWaitHandle’s constructor (so, if you want, you can use directly the EventWaitHandle class in your code, though that is not a general accepted practice).
Working with these kernel objects is similar to the ones we’ve seen before and I’ll leave the code for the next posts (I’m trying to write only short posts from now on because they’re easy to read and digest). Keep tuned for more!
Original Post: Multithreading: using events
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.