Thread: Major Multithread Problem :: MFC

  1. #1
    Registered User
    Join Date
    Nov 2001

    Major Multithread Problem :: MFC


    I just found a major multithread bug a program I working on. First, I will describe the process that is causes the major multithread problem.

    - user opens a text file (any size) w/ text only
    - user clicks an option in the menu
    - view has a executes its message handler for the menu item
    - view calls a function in doc
    - doc begins a new thread (thread includes CSinglelock + CriticalSection, data from doc)
    - thread resumes and begins processing the data
    - when done, thread *PostMessage* mainframe that is done ::: -> AfxGetMainWnd->PostMessage(WM_USER_THREAD, 0, 0);
    - main frame *SendMessage* to view ::: -> pView->SendMessage(WM_USER_THREAD, 0, 0)
    - view calls a function in doc to close thread
    - doc class this function
    // This function is called to close a thread
    m_bThread = true; // meaning thread is still active

    if (m_pThread && m_hThread) // m_hThread is a HANDLE to the thread
    ::WaitForSingleObject(m_hThread, INFINITE);
    delete m_pThread;
    m_pThread = NULL;
    m_hThread = NULL;
    m_bThread = false; // meaning thread has been closed



    Okay. Everything works good *if the use does not click anything* during the process. In other words, if the user were to click the menu or anything inside the window, the program would malfunction.

    Notice the bool "m_bThread." I use it as an indicator about the status of the thread. if m_bThread is true, meaning that the thread is active, the program disables many options in the menu. These options will remain disabled unless m_nThread is false. For some reason, that variable remains *true* if the user starts clicks anyone in the menu or view processing. That means something is keeping the program to ever setting m_Thread to false. On the other hand, if the user does not click anything until after the process is over, everything works as planned.

    I have no idea what is going on in the background. Is there a design flaw anywhere?


  2. #2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MFC problem
    By gadu in forum Windows Programming
    Replies: 5
    Last Post: 05-29-2009, 06:33 AM
  2. Creating and Empty MFC project in Visual Studio?
    By Swerve in forum Windows Programming
    Replies: 7
    Last Post: 11-01-2008, 04:43 PM
  3. Problem with MFC Wizard
    By okinrus in forum Windows Programming
    Replies: 1
    Last Post: 04-10-2004, 04:09 PM
  4. ListView ImageList problem (MFC)
    By The Dog in forum Windows Programming
    Replies: 1
    Last Post: 09-12-2002, 12:08 PM
  5. MFC is Challenging :: C++
    By kuphryn in forum C++ Programming
    Replies: 8
    Last Post: 02-05-2002, 01:33 AM