Re: ATL threading problem using client COM interface

Tech-Archive recommends: Speed Up your PC by fixing your registry



"Edward Diener" <eddielee_no_spam_here@xxxxxxxxxxxxxx> wrote in message news:uFgCM7sBJHA.528@xxxxxxxxxxxxxxxxxxxxxxx
Igor Tandetnik wrote:
"Edward Diener" <eddielee_no_spam_here@xxxxxxxxxxxxxx> wrote in message
news:%23WuM7vlBJHA.4172@xxxxxxxxxxxxxxxxxxxx
I ran into another problem, which I am still investigating, which has
to do with the window I am trying to update from my background
thread. I am passing a pointer to the CWindowImpl derived class which
represents the window I am trying to update to my background thread's
Execute function.
Attempts to change this window directly from the background thread by
calling a function in this class ( which calls DrawTextEx ) does not
succeed.

Don't do that. You can't draw on a window from a thread other than the one that created it. Post a message to it, and have the handler of that message do the drawing. Better still, update internal data structures appropriately, and call InvalidateRect. Eventually, WM_PAINT message will arrive: draw in response to that (which you have to do anyway).

Agreed. I think I have to call UpdateWindow after InvalidateRect to generate a WM_PAINT message.

No, InvalidateRect is enough. If you call UpdateWindow it will block your secondary thread until the main thread completes the update. This can also lead to deadlocks, for example if the main thread uses WaitForMultipleObjects.


I suspect this is because the actual window procedure for
this window is running on the main thread. Worse still, when I
attempt to close this window from the background thread, I get a
crash because the m_hWnd has not been set back to 0 in the window
procedure.

Again, post a message, let the main thread do the closing.

To get my window to update while my thread is running in the background it looks like my main thread, after starting my worker thread, needs to continue looping doing MsgWaitForMultipleObjectsEx / GetMessage / DispatchMessage until the worker thread is done. Then when the worker thread is done I can close the window in the main thread.

Using PostMessage from the secondary thread is preferable. It avoids possible deadlocks.

--
Scott McPhillips [VC++ MVP]

.



Relevant Pages

  • Re: ATL threading problem using client COM interface
    ... I think I have to call UpdateWindow after InvalidateRect to ... To get my window to update while my thread is running in the ... / GetMessage / DispatchMessage until the worker thread is done. ... Your main thread is an STA thread, ...
    (microsoft.public.vc.atl)
  • Re: ATL threading problem using client COM interface
    ... represents the window I am trying to update to my background thread's ... I think I have to call UpdateWindow after InvalidateRect to generate a WM_PAINT message. ... To get my window to update while my thread is running in the background it looks like my main thread, after starting my worker thread, needs to continue looping doing MsgWaitForMultipleObjectsEx / GetMessage / DispatchMessage until the worker thread is done. ...
    (microsoft.public.vc.atl)
  • Re: subclassing
    ... > call the SubclassWindow() function of the newly created object by ... > passing the window handle. ... > window procedure of the passed window with the Window procedure of the ... > CWindowImpl derived class. ...
    (microsoft.public.vc.atl)
  • Re: subclassing
    ... > call the SubclassWindow() function of the newly created object by ... > passing the window handle. ... > window procedure of the passed window with the Window procedure of the ... > CWindowImpl derived class. ...
    (microsoft.public.vc.atl)
  • Re: subclassing
    ... call the SubclassWindow() function of the newly created object by ... passing the window handle. ... window procedure of the passed window with the Window procedure of the ... CWindowImpl derived class. ...
    (microsoft.public.vc.atl)