0

I am building a method that uses the ManualResetEvent but i can't get it to run after i initiate the WaitOne method. Here is my code of the method. The code runs the code until it runs to the wait.WaitOne() call. Thanks!!

            var wait = new ManualResetEvent(false);
            Color tmpColor = new Color();
            MouseEventHandler tmpHandler = null;

            ThreadPool.QueueUserWorkItem(delegate
            {
                Debug.WriteLine("Adding MouseEventHandler..");
                tmpHandler = new MouseEventHandler(
                   (sender, e) =>
                   {
                       if (e.Button == MouseButtons.Left)
                       {
                           Bitmap tmpImage = new Bitmap(imgBox.Image);
                           tmpColor = tmpImage.GetPixel(e.X, e.Y);
                           Debug.WriteLine("Testing..");
                       }
                       else
                       {
                           Debug.WriteLine("Closing..");
                           this.Close();
                       }
                       wait.Set();
                   }
                );

                imgBox.MouseClick += tmpHandler;
            });


            Debug.WriteLine("Waiting..");

            wait.WaitOne();
            Debug.WriteLine("Running..");
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
chris
  • 343
  • 4
  • 13
  • 1
    That is expected behavior. What did you expect? – leppie Feb 17 '13 at 15:28
  • I used this example of a question that has been asked on this site. Ofcource i can't find the specific page again. But in the answer of the question came this solution and lookin by the comment of the original poster it worked for him. So i thought that i was doing something wrong. But @leppie do you have a possible solution where i get a working result of what i am trying to accomplish?? Thank you – chris Feb 18 '13 at 06:11

1 Answers1

1

It looks like you're blocking the main thread. This prevents event handlers from executing, thus your Set method never gets called.

Andrew Arnott
  • 80,040
  • 26
  • 132
  • 171
  • That depends on what you really mean to accomplish. Why are you blocking the main thread at all? Can you be asynchronous instead of synchronously blocking? – Andrew Arnott Feb 19 '13 at 23:18
  • What i want to acclomplish is that the method waits on the mouse click before it returns a color. I don't know the best way to realize this. And i was wondering if you or anyone else knows a wait to get this to work. Thank you – chris Feb 20 '13 at 07:32