Thread: Multithreading & Problem :: MFC

  1. #1
    Registered User
    Join Date
    Nov 2001

    Multithreading & Problem :: MFC


    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.


  2. #2
    Registered User
    Join Date
    Nov 2001

    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!.

    Special thanks to IndirectX for breaking up the subject about breakpoint.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non-MFC DLL with MFC app question.
    By Kempelen in forum Windows Programming
    Replies: 10
    Last Post: 08-20-2008, 07:11 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Problem with MFC Wizard
    By okinrus in forum Windows Programming
    Replies: 1
    Last Post: 04-10-2004, 04:09 PM
  4. Multithreading :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 10-03-2002, 08:39 AM
  5. ListView ImageList problem (MFC)
    By The Dog in forum Windows Programming
    Replies: 1
    Last Post: 09-12-2002, 12:08 PM