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. Ending Thread Problems

Ending Thread Problems

Scheduled Pinned Locked Moved C / C++ / MFC
questiondebuggingtutorial
5 Posts 3 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.
  • T Offline
    T Offline
    tbrake
    wrote on last edited by
    #1

    Hi I have some Debug and AccesViolation on teminating myThread. My code looks like: class MyAppView : public CScrollView { ... CMax * mMax; // A class containing a Dialog and a function // that will become a WorkerThread // The dialog is created Dynamic and mMax excists while // the the View excists bool maxbreak; // this is set to true from the View // the WorkerThread should terminate when set to true CWinThread * maxID; // handle to the workerthread ... }; ... // the workerthread UINT MaxThread(LPVOID pParam) { CMax* dlg = (CMax*)pParam; dlg->max(); return 0; } ... // calling the workerthread void MyAppView::MaxStart() { maxID = AfxBeginThread(MaxThread, mMax, THREAD_PRIORITY_IDLE ,0,CREATE_SUSPENDED); maxID->m_bAutoDelete=TRUE; maxID->ResumeThread(); } ... void MyAppView::OnKeyDown(UINT ..., UINT ..., UINT ...) { switch (nChar) { case VK_ESCAPE: if(!pauseMax) { maxbreak = TRUE; WaitForSingleObject(maxID,INFINITE); // delete maxID; // polls a AccessViolation } break; } } ... void CMax::max() { ... if(p_View) { p_View->SendNotifyMessage(...); } while(... && !maxbreak) { ... } if(p_View) { p_View->SendNotifyMessage(...); } ... } I hope u understand what i mean otherwise pls ask. I have 2 questions: 1. setting maxbreak = true ends prozessing imediatly but not the thread this takes a while to end up why and how to change ? 2. why does WaitForSingleObject(...) not wait for termination or how to explain the not working 'delete maxID'; THX

    R 1 Reply Last reply
    0
    • T tbrake

      Hi I have some Debug and AccesViolation on teminating myThread. My code looks like: class MyAppView : public CScrollView { ... CMax * mMax; // A class containing a Dialog and a function // that will become a WorkerThread // The dialog is created Dynamic and mMax excists while // the the View excists bool maxbreak; // this is set to true from the View // the WorkerThread should terminate when set to true CWinThread * maxID; // handle to the workerthread ... }; ... // the workerthread UINT MaxThread(LPVOID pParam) { CMax* dlg = (CMax*)pParam; dlg->max(); return 0; } ... // calling the workerthread void MyAppView::MaxStart() { maxID = AfxBeginThread(MaxThread, mMax, THREAD_PRIORITY_IDLE ,0,CREATE_SUSPENDED); maxID->m_bAutoDelete=TRUE; maxID->ResumeThread(); } ... void MyAppView::OnKeyDown(UINT ..., UINT ..., UINT ...) { switch (nChar) { case VK_ESCAPE: if(!pauseMax) { maxbreak = TRUE; WaitForSingleObject(maxID,INFINITE); // delete maxID; // polls a AccessViolation } break; } } ... void CMax::max() { ... if(p_View) { p_View->SendNotifyMessage(...); } while(... && !maxbreak) { ... } if(p_View) { p_View->SendNotifyMessage(...); } ... } I hope u understand what i mean otherwise pls ask. I have 2 questions: 1. setting maxbreak = true ends prozessing imediatly but not the thread this takes a while to end up why and how to change ? 2. why does WaitForSingleObject(...) not wait for termination or how to explain the not working 'delete maxID'; THX

      R Offline
      R Offline
      Ryan Binns
      wrote on last edited by
      #2

      tbrake wrote:

      explain the not working 'delete maxID'

      You've set m_bAutoDelete on the thread, so it is automatically deleted, and then that line is trying to delete it a second time.

      Ryan

      "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

      T 1 Reply Last reply
      0
      • R Ryan Binns

        tbrake wrote:

        explain the not working 'delete maxID'

        You've set m_bAutoDelete on the thread, so it is automatically deleted, and then that line is trying to delete it a second time.

        Ryan

        "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

        T Offline
        T Offline
        tbrake
        wrote on last edited by
        #3

        Hi Well equal if m_bAutoDelete = true or false calling delete maxID will last to an accessvalidation error. And do you anything aout ending threads quick ?? THX

        R 1 Reply Last reply
        0
        • T tbrake

          Hi Well equal if m_bAutoDelete = true or false calling delete maxID will last to an accessvalidation error. And do you anything aout ending threads quick ?? THX

          R Offline
          R Offline
          Ryan Binns
          wrote on last edited by
          #4

          tbrake wrote:

          And do you anything aout ending threads quick ??

          Not really, I let my threads manage their own lifetime. Much simpler and less prone to errors.

          Ryan

          "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

          B 1 Reply Last reply
          0
          • R Ryan Binns

            tbrake wrote:

            And do you anything aout ending threads quick ??

            Not really, I let my threads manage their own lifetime. Much simpler and less prone to errors.

            Ryan

            "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

            B Offline
            B Offline
            Blake Miller
            wrote on last edited by
            #5

            Exactly. If the thread does not end 'quickly' while doing its own work, then it is obviously making alot of synchronous calls, so then you need a redesign anyway, otherwise, it is just safest to wait. In between making calls to do wok, the thread should check an 'exit' flag - a variable set to tru or soemthing. While 'waiting' for something to finish, it should use synchronization functions. You might have to use overlapped IO or look for asynchronous methods and API for things yo are doing synchronously. Also, try adjusting timeouts on operations. If you know you are waiting, you can always tell user 'waiting for XXX to finish before shutting down' or some such message. In the case of Windows shutting down, well perhaps whatever you are waiting for will terminate, so your thread can finish up nicely anyway. People that start writing code immediately are programmers (or hackers), people that ask questions first are Software Engineers - Graham Shanks

            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