Problem with termination of a thread
-
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?
-
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?
Perhaps in your OnClose handler you could have... ShowWindow(SW_HIDE); WaitForSingleObject(m_hThread, INFINITE);
-
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?
-
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?
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
-
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?
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
-
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?
Maybe one of my previous posts can help: click here. - It's easier to make than to correct a mistake.