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. Problem with termination of a thread

Problem with termination of a thread

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structureshelp
6 Posts 6 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
    SWDevil
    wrote on last edited by
    #1

    Hi, I have an application, in which I run a thread when the application loads. This thread calls a certain function that initializes an array, and takes a few minutes to run. The thread works good, but if I close the application before the thread is done then there is an assertion. How can I overcome this? Can I maybe force the thread to end if the user closed the application before it finished?

    L J D Z J 5 Replies Last reply
    0
    • S SWDevil

      Hi, I have an application, in which I run a thread when the application loads. This thread calls a certain function that initializes an array, and takes a few minutes to run. The thread works good, but if I close the application before the thread is done then there is an assertion. How can I overcome this? Can I maybe force the thread to end if the user closed the application before it finished?

      J Offline
      J Offline
      Justin Tay
      wrote on last edited by
      #2

      Perhaps in your OnClose handler you could have... ShowWindow(SW_HIDE); WaitForSingleObject(m_hThread, INFINITE);

      1 Reply Last reply
      0
      • S SWDevil

        Hi, I have an application, in which I run a thread when the application loads. This thread calls a certain function that initializes an array, and takes a few minutes to run. The thread works good, but if I close the application before the thread is done then there is an assertion. How can I overcome this? Can I maybe force the thread to end if the user closed the application before it finished?

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #3

        SWDevil wrote:

        then there is an assertion.

        Is the assertion a Debug only mechanism? What is the behavior in "Release" mode?

        1 Reply Last reply
        0
        • S SWDevil

          Hi, I have an application, in which I run a thread when the application loads. This thread calls a certain function that initializes an array, and takes a few minutes to run. The thread works good, but if I close the application before the thread is done then there is an assertion. How can I overcome this? Can I maybe force the thread to end if the user closed the application before it finished?

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          SWDevil wrote:

          How can I overcome this?

          Read here.


          "The largest fire starts but with the smallest spark." - David Crow

          "Judge not by the eye but by the heart." - Native American Proverb

          1 Reply Last reply
          0
          • S SWDevil

            Hi, I have an application, in which I run a thread when the application loads. This thread calls a certain function that initializes an array, and takes a few minutes to run. The thread works good, but if I close the application before the thread is done then there is an assertion. How can I overcome this? Can I maybe force the thread to end if the user closed the application before it finished?

            Z Offline
            Z Offline
            Zac Howland
            wrote on last edited by
            #5

            You will need to do 2 things to do this cleanly: First, create an event and pass the event to the thread. Each pass of the Thread, use WaitForSingleObject with a low timeout value to see if the thread needs to exit (if it does, do any cleanup you need to and return from the thread). The first step will allow the thread to exit cleanly. In your main thread, you will need to also add a WaitforSingleObject and use the thread's handle as the object. Use a timeout value appropriate for you (INFINITE is NOT appropriate here as it will deadlock your application's shutdown procedures). If the Wait times out, call TerminateThread with the handle for the thread (this is a forceful killing of the thread). If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

            1 Reply Last reply
            0
            • S SWDevil

              Hi, I have an application, in which I run a thread when the application loads. This thread calls a certain function that initializes an array, and takes a few minutes to run. The thread works good, but if I close the application before the thread is done then there is an assertion. How can I overcome this? Can I maybe force the thread to end if the user closed the application before it finished?

              J Offline
              J Offline
              Jun Du
              wrote on last edited by
              #6

              Maybe one of my previous posts can help: click here. - It's easier to make than to correct a mistake.

              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