Re: MFC doc/view and threading
"neilsolent" wrote:
Hi
Can anyone explain this behaviour?
I have a doc/view MFC program. When it initializes, I
launch a
background thread (the idea is that the original thread
processes the
GUI and the background thread does other non-GUI tasks).
At this
point, Task manager is showing 2 threads for the process..
as I would
expect.
Now .. when the user starts an application-modal dialog,
Task manager
is suddenly showing 4 threads! When that dialog is
dismissed, there
are still 4 threads showing!
I have also noticed that, when the dialog is running, the
other
windows are still able to update (they process all
messages,
normally).
The questions are .. what is the threading model behind
all this?
Should I not be worried about thread safety? Presumably
there is some
message-processing loop in the background? Why the 4
threads??
It is likely that your program uses one or more COM
components. When COM (or OLE) object is instantiated, then
COM subsystem can create some threads for its needs. You
should not worry about them, since COM subsystem manages
these threads internally and it doesn't interfere with your
threads. (As an example, you can launch Notepad - it will
start with one thread initially. Then select File -> Open
menu item. When "Open File" common dialog appears, then
number of threads within Notepad process increases.)
Considering modal dialogs, they don't stop message
processing of other windows in an application. As a simple
test you can show modal dialog and move it with mouse.
Parent window will be repainted correctly. It indicates that
parent window continues to recieve messages and processes
them. This is quote from MSDN:
"About Dialog Boxes", section "Modal Dialog Boxes"
http://msdn2.microsoft.com/en-us/ms644994.aspx
<quote>
To process messages for the modal dialog box, the system
starts its own message loop, taking temporary control of the
message queue for the entire application. When the system
retrieves a message that is not explicitly for the dialog
box, it dispatches the message to the appropriate window.
</quote>
HTH
Alex