Solved

Pumping messages during Thread.Join(myTimeout)

Posted on 2008-10-10
1
459 Views
Last Modified: 2013-12-17
Hi everybody,

I use a thread to wait until an event occurs.
I create and lauunch the thread, than wait until the threads ends or a timeout is reached.
The problem is (I think), that during Thread.Join(myTimeout) there is no messages pumping, and so no event can be catched !
How could I pump messages ?
Or should I use anything else that Thread.Join method ?

Thank you very much,
Vincent
....

Thread WaitUntilEventThread = new Thread(new ThreadStart(WaitEventService));

WaitUntilEventThread.Start();

WaitUntilEventThread.Join(myTimeout);

}
 

private void WaitEventService()

{

     while(!isMacroComplete)

     {

          Thread.Sleep(500);

     }

}
 

private void Pathway_MacroComplete(object sender, MyLib.MyEvent eventArgs)

{

      isMacroComplete = true;   

}

Open in new window

0
Comment
Question by:vinceTrace
1 Comment
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 22688126
Right...there is no point in making a new thread if you are just going to Join() them...   =\

If you really want to force the pumping of messages then use "Application.DoEvents()".

*** BUT *** if you do this then there is really no need for the separate thread is there?

Just use a polling loop to look for the toggling of your flag:

    // some code
    while(!isMacroComplete)
     {
          Thread.Sleep(100); // use a smaller number than 500 or your UI will be sluggish!
          Application.DoEvents();
     }
     // some more code that needs to run after "isMacroComplete" toggles

If you really want to use a different thread that doesn't require DoEvents() then encapsulate the while loop in a class that raises an EVENT when the flag is toggled.  This would require a change in your code methodology though as you would be moving from linear code to event driven code.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now