Archived

This topic is now archived and is closed to further replies.

kuphryn

Multithreading & Problem :: MFC

Recommended Posts

kuphryn    210
Hi. I have discovered a problem with multithreading processes in my program. Here is an example. - program starts - CMyProgramApp::InitInstance() calls RestoreState() in CMainFrame() - RestoreState() restores the windows positions via registry - RestoreState() restores data (int, char, binary, etc) from registry to variables in CMyProgramDoc() - RestoreState() calls a function in CMyProgramDoc() which begins a worker thread - worker thread does some calculation Problem: For some reason, I have found that if the calculation is big i.e. it takes a long time, the thread never gets closed and deleted. The worker thread PostMessage main frame a message indicating that it is done. However, sometimes main frame never received the message! ----- UINT WorkerThreadFunc(LPVOID pParam) { ... pWnd->PostMessage(WM_USER_WORKERTHREAD_CLOSED, 0, 0); return 0; ----- There is a handler in main frame to handle the message that class a function in doc to close the worker thread; however, I found that if the worker thread takes a long time to finish a calculation, main frame never receive or ignores the PostMessage. The only problem I can think of right now is I am starting the worker thread too early. In other words, I should start the worker thread after the program has started and all window positions have been restored. Do you see any other flaws? Please post. Thanks, Kuphryn

Share this post


Link to post
Share on other sites
IndirectX    122
Try setting a breakpoint on PostMessage and see if pWnd has the valid window handle. Also, try SendMessage instead of PostMesssage and see if the window actually gets that message (set a breakpoint on pWnd''s WndProc and see if it''s hit before SendMessage returns).

Share this post


Link to post
Share on other sites
cgoat    132
The obvious question is:

Are you sure the WorkerThread is ending and actually posting the message?

I don't see anything wrong with using a pointer and doing pWnd->PostMessage, but perhaps you should try passing the HWND instead to the worker thread, then calling :ostMessage(HWND, MESSAGE, 0, 0) instead, as this is how the documentation says to communicate with the parent window. MFC keeps track of which thread owns what, so maybe it won't let you access the message queue of the other window that way.

[edited by - cgoat on June 6, 2002 2:42:35 PM]

Share this post


Link to post
Share on other sites
kuphryn    210
Okay. Thanks for your pointer about breakpoint.

Damn. The problem was not anything except bad debugging. I made a small mistake that caused a program loop inside the worker thread. The worker thread never PostMessage! It there is a loop that never exits.

The solution was to reset one of the variables I used inside the while look.

The breakpoint feature in Visual C++ is extremely useful!.

Kuphryn

Share this post


Link to post
Share on other sites
kuphryn    210
Yeah. I know. I read studies it well from Jeff Prosise. Fortunately, PostMessage and SendMessage work okay in all threads.

Kuphryn

Share this post


Link to post
Share on other sites