Solved

Pumping messages during Thread.Join(myTimeout)

Posted on 2008-10-10
1
463 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 86

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

739 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