Optimizing Wait in MultiThreading

While working in Multithreading environment the primary thread has to wait until it’s secondary thread to complete their execution. So in this case most of the time we use Thread.Sleep() and that is a bad practice because it blocks the primary thread for a time that might be less or exceeds than the completion time of secondary thread.  You can optimize the waiting time in multithreading by using System.Threading.AutoResetEvent class.
To achieve this we’ll utilize the WaitOne() method of AutoResetEvent class. it’ll wait until the it gets notified from secondary thread for completion and This I think should be the optimized way for wait here.
Let see the example:
using System;
using System.Threading;
namespace Threads_CSHandler_AutoResetEvent
{
    class Program
    {
        //Declare the wait handler
        private static AutoResetEvent waitHandle = new AutoResetEvent(false);

        static void Main(string[] args)
        {
            Console.WriteLine("Main() running on thread {0}", 
                              Thread.CurrentThread.ManagedThreadId);

            Program p = new Program();
            Thread t = new Thread(new ThreadStart(p.PrintNumbers));
            t.Start();
            //Wait untill get notified
            waitHandle.WaitOne();
            Console.WriteLine("\nSecondary thread is done!");
            Console.Read();
        }

        public void PrintNumbers()
        {
            Console.WriteLine(" ");
            Console.WriteLine("Executing Thread {0}", 
                               Thread.CurrentThread.ManagedThreadId);
            for (int i = 1; i <= 10; i++)
            {
                //Just to consume some time in operation
                Thread.Sleep(100);
                Console.Write(i + " ");
            }
            //Tell other other thread that we're done here
            waitHandle.Set();
        }
    }
}

This entry was posted on Wednesday, January 4th, 2012 at 9:14 am and is filed under C# ASP .NET. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

63 Responses to “Optimizing Wait in MultiThreading”

  1. Snappointx Says:

    Skipflyx…

    Fantastic blog post, saw on…

  2. internet explorer 8 download for mac Says:

    internet explorer 8 download for mac…

    […]Heard about this website from my buddy. He pointed me right here and informed me I’d discover what I need. He was correct! I acquired all of the questions I had, answered. Did not even get long to find it. Love the truth that you produced it so e…

  3. old silver dollars Says:

    That’s Right!…

    This is a really good blog. Good work!…

  4. forex broker Says:

    Hi…

    I believe other website proprietors should take this website as an model, very clean and good user pleasant layout. “Only I can change my life. No one can do it for me.” by Carol Burnett….

  5. tinnitus yoga Says:

    Your Link Found In Gaming Blog…

    […]What sites аnd blogs do thе surfing community communicate most on?[…]…

  6. Odszkodowanie powypadkowe Says:

    Good…

    Real nice layout and superb content, very little else we require :D….

  7. Forex Says:

    Thanks…

    I truly appreciate this post. I have been looking all over for this! Thank goodness I found it on Bing. You have made my day! Thank you again!…

  8. Dentysta w londynie Says:

    OK…

    Very interesting points you have noted, thank you for posting….

  9. Forex Says:

    Great…

    Very interesting points you have observed , thanks for putting up. “I’ve made a couple of mistakes I’d like to do over.” by Jerry Coleman….

  10. Apartamenty Zakopane Says:

    Helpful…

    Wonderful web site. Plenty of helpful info here. I’m sending it to a few pals ans also sharing in delicious. And obviously, thank you for your effort!…

  11. Forex Says:

    Good…

    I like the valuable information you provide in your articles. I will bookmark your blog and check again here regularly. I am quite sure I’ll learn a lot of new stuff right here! Best of luck for the next!…

  12. Strony www Says:

    Good…

    I like this weblog so much, bookmarked.

  13. Snowboardzik Says:

    Nice…

    excellent submit, very informative. I\’m wondering why the other specialists of this sector don\’t realize this. You should proceed your writing. I\’m sure, you\’ve a huge readers\’ base already!…

  14. pozycjonowanie stron internetowych Says:

    ok…

    It is super weblog, I wish to be like you…

  15. kwiaty Says:

    cebulki kwiatowe…

    very interesting info ! ….

  16. kwiaty Says:

    bonsai…

    I don’t commonly comment but I gotta tell thanks for the post on this great one : D….

  17. Ogrodzenia Says:

    Ogrodzenia Plastikowe…

    Recently, I did not give a lot of consideration to leaving comments on blog web page posts and have positioned feedback even much less….

  18. obrazy nowoczesne Says:

    obrazy…

    Thank you so much for giving everyone an exceptionally remarkable opportunity to discover important secrets from this web site. It is often so fantastic and packed with amusement for me personally and my office fellow workers to search your blog nearly…

  19. ogrodzenia plastikowe Says:

    sztachety…

    WONDERFUL Post.thanks for share..extra wait .. ……

  20. Kwiaty Ozdobne Says:

    Kwiaty…

    Thanks, this really is the worst factor I’ve study…

  21. sztachety Says:

    sztachety…

    Thanks for what you have got. This really is the most beneficial post I’ve study…

  22. sztachety plastikowe Says:

    sztachety plastikowe…

    Weblog moved out in chrome…

  23. archiwizacja online Says:

    archiwizacja…

    Hey, buddy, I have not figured out how you can subscribe…

  24. archiwizacja Says:

    archiwizacja…

    Good post. I study something more difficult on different blogs everyday. It should at all times be stimulating to learn content material from different writers and apply a little one thing from their store. I’d prefer to use some with the content mater…

  25. ogrodzenia Says:

    ogrodzenia plastikowe…

    I’m usually to running a blog and i actually admire your content. The article has actually peaks my interest. I’m going to bookmark your website and keep checking for brand spanking new information….

  26. ogrodzenia dla koni Says:

    sztachety…

    Spot on with this write-up, I actually think this web site needs way more consideration. I’ll most likely be once more to learn rather more, thanks for that info….

  27. ogrodzenia plastikowe Says:

    sztachety plastikowe…

    Nice post. I be taught something more challenging on totally different blogs everyday. It is going to all the time be stimulating to read content from other writers and apply a little bit something from their store. I’d desire to use some with the cont…

  28. archiwizacja online Says:

    backup…

    That you are my function designs. Thanks for that write-up…

  29. backup Says:

    backup…

    I critically delight in your posts. Many thanks…

  30. backup Says:

    archiwizacja…

    I intended to post you a tiny remark to finally say thanks as before with the gorgeous solutions you have featured here. It’s so tremendously generous with people like you to supply without restraint exactly what a lot of folks could possibly have sol…

  31. 1921 american silver dollar Says:

    Thanks!…

    Thanks for all your insight. This site has been really helpful to me….

  32. Strona o ekonomi Says:

    OK…

    thanks for helpful recommendations and merely excellent info…

  33. Cheap SEO Packages Says:

    Full Report…

    […]Oh my goodness! Incredible article dude! Thank you, However I am encountering difficulties with your RSS. I don’t know the reason why I am unable to subscribe to it. Is there anybody getting similar RSS issues? Anyone that knows the answer will yo…

  34. sztachety plastikowe Says:

    sztachety…

    I can not subscribe for your channel…

  35. admin Says:

    what is the issue you are facing…

  36. arizonacarinsurance.info Says:

    arizonacarinsurance.info…

    An interesting post over at……

  37. michigancarinsurance.info Says:

    I saw this blog come up on Google reader today, have you……

  38. great list Says:

    The information mentioned in the article are some of the best available…

  39. sztachety Says:

    ogrodzenia dla koni…

    Hi! Your write-up rocks also as becoming a respectable fantastic fully grasp!??…

  40. obrazy olejne Says:

    obrazy…

    Hey, buddy, I’ve not discovered the best way to subscribe…

  41. ogrodzenia Says:

    ogrodzenia…

    You are my role designs. Thank you for that article…

  42. Bonsai Says:

    Cebule Kwiatów…

    I am usually to blogging and i actually recognize your content. The article has actually peaks my interest. I’m going to bookmark your site and hold checking for brand spanking new information….

  43. Nvidia Geforce 6200 Says:

    click to read…

    […]the time to read or visit the content or sites we have linked to below the[…]…

  44. Gold Buyers At The Mall Scam Says:

    Tumblr article…

    I saw a writer talking about this on Tumblr and it linked to…

  45. serwis drukarek Says:

    serwis ploterów Poznań…

    in the event you want, I’ll compose you content articles. Copywriter searching for operate…

  46. Cebulki Kwiatowe Says:

    Cebulki Kwiatów…

    This can be a set of words, not an essay. you’re incompetent…

  47. węgiel kamienny Says:

    węgiel Poznań…

    Oh my goodness! an incredible article dude. Thanks Nevertheless I am experiencing concern with ur rss . Don’t know why Unable to subscribe to it. Is there anyone getting an identical rss drawback? Anybody who is aware of kindly respond. Thnkx…

  48. backup danych Says:

    backup online…

    I can’t subscribe for your channel…

  49. serwis drukarek Says:

    naprawa ploterów…

    It is tremendous weblog, I want to be like you…

  50. Cebule Kwiatów Says:

    Cebulki Kwiatów…

    r u certain that is certainly accurate?…

  51. Sztachety Plastikowe Says:

    Ogrodzenia…

    There are some fascinating time limits on this article but I don’t know if I see all of them heart to heart. There’s some validity however I’ll take maintain opinion till I look into it further. Good article , thanks and we would like extra! Added to…

  52. kourtney kardashian baby Says:

    Here is something I found out today:…

    I recommend you check out this magnificent article…

  53. PAR20 LED lights Says:

    Wow, great blog post.Really thank you! Great….

    I really like and appreciate your blog.Really looking forward to read more. Really Great….

  54. serwis ploterów Says:

    Ogrodzenia…

    Youre so cool! I dont suppose Ive read anything like this before. So nice to search out somebody with some unique ideas on this subject. realy thanks for beginning this up. this web site is one thing that’s needed on the net, someone with a bit of ori…

  55. Ogrodzenia Says:

    Ogrodzenia…

    From: Ogrodzenia I am a long time ago I study your blog and it has extended been saying that you’re a great author…

  56. Ogrodzenia Says:

    Ogrodzenia…

    It’s onerous to search out educated individuals on this topic, but you sound like you understand what you’re speaking about! Thanks…

  57. Ogrodzenia Says:

    Ogrodzenia…

    I can’t determine how do I subscribe to your blog Ogrodzenia…

  58. Ogrodzenia Says:

    Ogrodzenia…

    Lately, I did not give plenty of consideration to leaving feedback on blog Ogrodzenia page posts and have placed responses even substantially much less….

  59. hello world Says:

    Title…

    […]The details talked about within the post are a few of the most effective accessible […]…

  60. The Unemployed Mom Says:

    Source…

    I like the helpful info you provide in your articles. I’ll bookmark your blog and check again here frequently. I’m quite sure I will learn a lot of new stuff right here! Good luck for the next!…

  61. US Economy Says:

    Website…

    […]Hello there, There’s no doubt that your site might be having internet browser compatibility issues. Whenever I look at your site in Safari, it looks fine however when opening in Internet Explorer, it has some overlapping issues. I simply wanted t…

  62. Jame Jorden Says:

    Woman of Alien…

    Best perform you might have done, this great site is basically amazing with superb facts. Time is God’s way of trying to keep almost everything from occurring simultaneously….

  63. You sir, are awesome! Says:

    Hello…

    Only wanna comment that you have a very decent site, I love the style it actually stands out….

Leave a Reply

*