Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Win7 - worker thread freeze on call to WaitForSingleObject

Win7 - worker thread freeze on call to WaitForSingleObject

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++lounge
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stan the man
    wrote on last edited by
    #1

    Hi All. I call the following when I need to save a report in my MFC base program: m_pEndThread = AfxBeginThread(Save2CWGCoilRep, &m_CoilRep[iCRepPos], THREAD_PRIORITY_NORMAL,0,0); UINT Save2CWGCoilRep(LPVOID pParam) { I use CStdioFile to them save data to file } Then before I save another report, I will call if(m_pEndCoilThread != NULL) ::WaitForSingleObject(m_pEndCoilThread->m_hThread, INFINITE); to see if the thread is finished before calling again to save another report. And if not just wait. It seems in Win7 now, this call will not exit so that the program will just freeze. It does not freeze all the time, seems random. In WinXP, the same program seems not to have this problem. I am not sure what I am missing. Any help or suggestion will be appreciated. Thanks! Stan the man

    J B L 3 Replies Last reply
    0
    • S Stan the man

      Hi All. I call the following when I need to save a report in my MFC base program: m_pEndThread = AfxBeginThread(Save2CWGCoilRep, &m_CoilRep[iCRepPos], THREAD_PRIORITY_NORMAL,0,0); UINT Save2CWGCoilRep(LPVOID pParam) { I use CStdioFile to them save data to file } Then before I save another report, I will call if(m_pEndCoilThread != NULL) ::WaitForSingleObject(m_pEndCoilThread->m_hThread, INFINITE); to see if the thread is finished before calling again to save another report. And if not just wait. It seems in Win7 now, this call will not exit so that the program will just freeze. It does not freeze all the time, seems random. In WinXP, the same program seems not to have this problem. I am not sure what I am missing. Any help or suggestion will be appreciated. Thanks! Stan the man

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      You may ran into a race condition. A common way to check if a thread has been terminated (ensure that STILL_ACTIVE is not returned by your thread):

      if (m_pEndCoilThread != NULL &&
      m_pEndCoilThread->m_hThread != INVALID_HANDLE_VALUE)
      {
      DWORD dwExitCode = 0;
      ::GetExitCodeThread(m_pEndCoilThread->m_hThread, &dwExitCode);
      if (dwExitCode == STILL_ACTIVE)
      ::WaitForSingleObject(m_pEndCoilThread->m_hThread, INFINITE);
      m_pEndCoilThread = NULL;
      }

      S 1 Reply Last reply
      0
      • S Stan the man

        Hi All. I call the following when I need to save a report in my MFC base program: m_pEndThread = AfxBeginThread(Save2CWGCoilRep, &m_CoilRep[iCRepPos], THREAD_PRIORITY_NORMAL,0,0); UINT Save2CWGCoilRep(LPVOID pParam) { I use CStdioFile to them save data to file } Then before I save another report, I will call if(m_pEndCoilThread != NULL) ::WaitForSingleObject(m_pEndCoilThread->m_hThread, INFINITE); to see if the thread is finished before calling again to save another report. And if not just wait. It seems in Win7 now, this call will not exit so that the program will just freeze. It does not freeze all the time, seems random. In WinXP, the same program seems not to have this problem. I am not sure what I am missing. Any help or suggestion will be appreciated. Thanks! Stan the man

        B Offline
        B Offline
        bjorn_ht
        wrote on last edited by
        #3

        From the code example I take it that you create the Save2CWGCoilRep thread from the UI thread, i.e. from a menu or button handler function. My guess then is that you have a deadlock: 1. The background thread sends messages to the UI thread. Maybe it tries to display a message box, but it could be anything really. 2. The UI thread is suspended in WaitForSingleObject and can't process the message. It's risky to suspend the UI thread as you don't know when or why messages are posted to it. I would instead protect the critical parts of Save2CWGCoilRep with a critical section and take away the WaitForSingleObject call in the UI thread.

        1 Reply Last reply
        0
        • J Jochen Arndt

          You may ran into a race condition. A common way to check if a thread has been terminated (ensure that STILL_ACTIVE is not returned by your thread):

          if (m_pEndCoilThread != NULL &&
          m_pEndCoilThread->m_hThread != INVALID_HANDLE_VALUE)
          {
          DWORD dwExitCode = 0;
          ::GetExitCodeThread(m_pEndCoilThread->m_hThread, &dwExitCode);
          if (dwExitCode == STILL_ACTIVE)
          ::WaitForSingleObject(m_pEndCoilThread->m_hThread, INFINITE);
          m_pEndCoilThread = NULL;
          }

          S Offline
          S Offline
          Stan the man
          wrote on last edited by
          #4

          Hi Jochen. Thanks. I will try it out.... Stan

          1 Reply Last reply
          0
          • S Stan the man

            Hi All. I call the following when I need to save a report in my MFC base program: m_pEndThread = AfxBeginThread(Save2CWGCoilRep, &m_CoilRep[iCRepPos], THREAD_PRIORITY_NORMAL,0,0); UINT Save2CWGCoilRep(LPVOID pParam) { I use CStdioFile to them save data to file } Then before I save another report, I will call if(m_pEndCoilThread != NULL) ::WaitForSingleObject(m_pEndCoilThread->m_hThread, INFINITE); to see if the thread is finished before calling again to save another report. And if not just wait. It seems in Win7 now, this call will not exit so that the program will just freeze. It does not freeze all the time, seems random. In WinXP, the same program seems not to have this problem. I am not sure what I am missing. Any help or suggestion will be appreciated. Thanks! Stan the man

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Where does m_pEndCoilThread come from? DOnt you mean m_pEndThread?

            ============================== Nothing to say.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups