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);
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.