Re: ATL threading problem using client COM interface
- From: "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp>
- Date: Mon, 25 Aug 2008 13:31:03 -0400
"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]
.
- Follow-Ups:
- Re: ATL threading problem using client COM interface
- From: Edward Diener
- Re: ATL threading problem using client COM interface
- References:
- Re: ATL threading problem using client COM interface
- From: Igor Tandetnik
- Re: ATL threading problem using client COM interface
- From: Igor Tandetnik
- Re: ATL threading problem using client COM interface
- From: Edward Diener
- Re: ATL threading problem using client COM interface
- From: Igor Tandetnik
- Re: ATL threading problem using client COM interface
- From: Edward Diener
- Re: ATL threading problem using client COM interface
- From: Igor Tandetnik
- Re: ATL threading problem using client COM interface
- From: Edward Diener
- Re: ATL threading problem using client COM interface
- Prev by Date: Re: ATL threading problem using client COM interface
- Next by Date: Re: ATL threading problem using client COM interface
- Previous by thread: Re: ATL threading problem using client COM interface
- Next by thread: Re: ATL threading problem using client COM interface
- Index(es):
Relevant Pages
|