Re: Thread.Sleep vs Thread.Join

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"Joe" <joeusenet@xxxxxxxxx> wrote in message
news:1142902530.953063.284660@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
| I've played around more and found that windows timers behave
| differently when blocked on a sleep vs join. In the program below,
| left-clicking once pauses 5 seconds then starts adding items to the
| listbox, while right-clicking pauses 5 seconds then adds 5 items to the
| listbox (all with the current time). In other words, timer events queue
| up while blocked on a Join, but not while blocked on Sleep.
|

There is no message pumping/dispatching while the UI thread is 'Sleeping'.
On the other end, when you call Join on the current thread you still pump
messages, that means that your Timer messages are getting dispatched when
they arrive, the only thing that's not been handled are the painting
messages, that's why the list is only repainted after the Join time-out
period.

| using System;
| using System.Windows.Forms;
|
| class SleepJoinForm : Form
| {
| Timer tmr = new Timer ();
| ListBox lb = new ListBox ();
|
| public SleepJoinForm ()
| {
| tmr.Interval = 1000;
| tmr.Tick += delegate { lb.Items.Add (DateTime.Now); };
|
| lb.Dock = DockStyle.Fill;
| Controls.Add (lb);
| lb.MouseDown += delegate (object sender, MouseEventArgs e)
| {
| tmr.Start ();
| if (e.Button == MouseButtons.Left)
| System.Threading.Thread.Sleep (5000);
| else
| System.Threading.Thread.CurrentThread.Join (5000);
| };
| }
|
| [STAThread]
| static void Main () { Application.Run (new SleepJoinForm ()); }
| }
|
| Interestingly if you replace STAThread with MTAThread, the Join-wait
| behaves just like the Sleep-wait. I think you're right in that it's
| related to message pumping.
|
| I wonder if it's possible for a similar issue to crop up in a non-UI
| application? Unfortunately I'm largely ignorant of COM and message
| pumping.
|


That's what I said in my previous response, the CLR pumps the queue only
when Join is called on a STA thread, otherwise it behaves 'like' a Sleep.
You see there are two pre-requisites, you need a thread with a message queue
that runs in a STA. All UI threads have a message queue and they should run
in a STA, a non UI thread that hosts a apartment threaded COM object, must
run in a STA AND MUST pump the message queue. The CLR performs a (limited)
pumping wait when one of the following API's; Monitor.Enter (lock in C#),
Thread.Join, WaitOne, GC.WaitForPendingFinalizers are called from a STA
thread that has a message queue (a window really) attached.
A non UI STA thread that creates an instance of COM object MUST pump the
message queue. Failing to pump, will block the finalizer thread when one
releases the reference to the COM object(s). That means that at least you
should call WaitForPendingFinalizers after you released the Object
reference.

Willy.








.



Relevant Pages

  • Re: ASP calls MTA calls STA...
    ... If I have a VB Application which uses my MTA then the timer event callback ... it in the ASP application object then my STA timer does not get call backs. ... The STA is using a normal Windows::SetTimer call to create the timer. ...
    (microsoft.public.vc.atl)
  • Re: Monitoring a well pump on-times??
    ... Get a light timer with an electromechanical movement. ... give you 110 whenever the pump is on, and plug the timer into that. ... the house indication that the pump is running. ...
    (sci.electronics.design)
  • Re: Outdoor Outlet off 220v pool pump
    ... soon to be replaced with a timer. ... I just put in a large gazebo in the same area as the gangbox. ... say a boombox radio in the gazeebo at night while the pool pump is ... then use a step-down transformer in that. ...
    (alt.home.repair)
  • Re: Monitoring a well pump on-times??
    ... The well head shed is about 100 yards from the house and not visible ... Get a light timer with an electromechanical movement. ... give you 110 whenever the pump is on, and plug the timer into that. ... An indirect way that has the additional benefit of typically saving 10% of your electricity bill is to monitor the entire house current consumption in realtime using one of the clamp on wireless OWL meters. ...
    (sci.electronics.design)
  • Re: How to prevent caption bar from blocking message loop?
    ... xrxst32 wrote: ... think this behaviour (blocking message queue) has a different source. ... What about putting the timer in a separate UI thread, and using SendMessageto get back to the main thread, and then UpdateWindowto get rid of need for a WM_PAINT message? ...
    (microsoft.public.vc.mfc)