Re: "Critical Section" for a single thread

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Alexander Grigoriev (alegr_at_earthlink.net)
Date: 07/15/04


Date: Wed, 14 Jul 2004 19:21:59 -0700

OnTimer functions are called synchronously, from the message loop. You don't
need to protect from them. There is no reentrancy, _unless_ you make a
secondary message loop, for example, by showing a modal dialog.

In general, you should avoid waiting for an object in OnTimer handlers,
because it stops the message loop.

"Don McBride" <Don McBride@discussions.microsoft.com> wrote in message
news:576EC1BF-2BDD-48D4-9329-361F93B3A91E@microsoft.com...
> In short, here's my question: I'm familiar with Critical Sections, but
that only applies when two or more threads are competing for the same
resource. What mechanism is available when a single thread is competing
with itself ( due to OnTimer() ) for the same resource?
>
> I have an application which communicates with an instrument via USB port.
In the application there are several windows open simultaneously. Most
windows have a timer. Some of the OnTimer() routines communicate with the
instrument. Obviously, only one routine can communicate with the instrument
at a time. Unfortunately, since the timers operate asynchronously, one
OnTimer() may want to access the instrument while another OnTimer() is
already in the process of doing so. To insure the information is return to
the correct caller, I make a calling routine wait until the current
communication is through. The problem is the since OnTimer() does not
generate a new thread, there can be two parts of the code waiting for an
single event. The initial request must have somehow given the Run Message
Pump an occasion to dispatch the OnTimer() message before the USB
communication was completed. ( i.e. the message pump must have dispatched
an OnTimer() message during a USB communication.) The result is that the
communication can never finish because even though the scheduler is giving
the thread the proper CPU time, the thread is in a part of the code which is
waiting for another part to finish, but that part never gets executed.
Hence, my initial question.
>