Skip to main content
gamedev.net
🔒 Locked

Multithreading & Problem :: MFC

Started by kuphryn Jun 6, 2002 at 12:49 PM 4 replies 400+ views
Original Post
kuphryn
kuphryn
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
IndirectX
IndirectX
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).
---visit #directxdev on afternet <- not just for directx, despite the name
cgoat
cgoat
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]
kuphryn
kuphryn
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

Shannon Barber
Shannon Barber
You''re kind not suppose to pass CWnd pointers between threads - some function works, some don''t.
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
kuphryn
kuphryn
Yeah. I know. I read studies it well from Jeff Prosise. Fortunately, PostMessage and SendMessage work okay in all threads.

Kuphryn

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.