CSharpFeeds - All your C# feeds in one place.

Sponsors

Tuesday, May 19, 2009

Multithreading: ManualResetEvent events

by luisabreu via LA.NET [EN] on 5/19/2009 9:17:31 AM

Yesterday we’ve taken a peek at the AutoResetEvent. Today, we’re going to keep looking at kernel objects and we’ll see how we can use the ManualResetEvent for synchronization purposes. I guess that the easiest way to show them doing some work is by going straight to the code.

Yesterday we’ve taken a look at a really dumb sample that showed the behavior of the AutoResetEvent class. Today, we’re porting (sort of) that sample so that it uses the ManualResetEvent class:

using (var evt = new ManualResetEvent(false)) {   
    var threads = Enumerable.Range(0, 2)
        .Select(i => new Thread(() => {
                                        evt.WaitOne();
                                        Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
                                    }))
        .ToList();
    foreach (var thread in threads) {
        thread.Start();
    }
    Console.WriteLine("Fired threads");
    Thread.Sleep(200);
    evt.Set();
    Console.ReadLine();
}

As you can see, we start by creating a new ManualResetEvent in the non signaled state. Then, we create and start two threads which block until the event is set. As you can see, we don’t need to set the event in the secondary threads (like we did in yesterday’s sample). That happens because the event isn’t automatically reset after it has been set. If we needed that, then we would need to call the Reset method. Unfortunately, I’m out of time to present a more interesting sample, but I’ll return to the topic in a future post. Keep tuned.


email it!bookmark it!digg it!

Original Post: Multithreading: ManualResetEvent events

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

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.

Advertise with us