Termination of a thread
-
Hi, I use a thread, whitch I have created with 'AfxBeginThread'. Within this thread I use an endless while loop [while(1) {//do some thing}]. Is it possible to terminate this thread by the thread-caller? All I could find is to use the TerminateThread function but to do this I have to use before the DuplicateHandle function. Unfortunately I was realy not very successfull by trying this. Can anybody help please? Thanks a lot Vassili
-
Hi, I use a thread, whitch I have created with 'AfxBeginThread'. Within this thread I use an endless while loop [while(1) {//do some thing}]. Is it possible to terminate this thread by the thread-caller? All I could find is to use the TerminateThread function but to do this I have to use before the DuplicateHandle function. Unfortunately I was realy not very successfull by trying this. Can anybody help please? Thanks a lot Vassili
If it is a worker thread, you simply need to exit the thread function. If it is a UI thread, you need to post a WM_QUIT message.
-
Hi, I use a thread, whitch I have created with 'AfxBeginThread'. Within this thread I use an endless while loop [while(1) {//do some thing}]. Is it possible to terminate this thread by the thread-caller? All I could find is to use the TerminateThread function but to do this I have to use before the DuplicateHandle function. Unfortunately I was realy not very successfull by trying this. Can anybody help please? Thanks a lot Vassili
The sensible way to terminate a thread is to make it terminate itself - generally by setting some sort of flag to signal that the thread should terminate. In its simplest form, the code looks something like this:
//assumes your bTerminateFlag variable is defined elsewhere - you'd
//probably actually do this with some sort of class, and pass a pointer
//to the class instance to your thread function, but I'm keeping this simple
extern bool bTerminateFlag;int YourThreadFunction(LPVOID pParam)
{
while (!bTerminateFlag)
{
do_something...
}
return exit_code;
}And your function to terminate the thread would look something like this:
int TerminateWorkerThread(HANDLE hWorker)
{
bTerminateFlag = true;
if (WaitForSingleObject(hWorker, INFINITE)!=WAIT_OBJECT_0)
{
//an error has occurred
//handle it somehow
}
else
{
return GetExitCodeThread(hWorker);
}
}This is all very simplified - you'd want proper error checking, you probably shouldn't use a global variable as the terminate flag, and you shouldn't really use WaitForSingleObject in a thread with a message loop. But the principle is there. There's generally very little reason to use TerminateThread, and it can lead to all sorts of problems when your thread is doing something worthwhile, and holding resources open, because the terminated thread doesn't get to do any cleanup.
"We are the knights who say Ni" (The Knights Who Say Ni)
-
If it is a worker thread, you simply need to exit the thread function. If it is a UI thread, you need to post a WM_QUIT message.
Yes, it is a worker thread. In my endless while loop I read the serial port. On an port event I send a message to the main program. I also send a message to the main program with the handle of the thread by using GetCurrentThread() UINT TheThread(LPVOID pParam) { HANDLE* pObject = (HANDLE*)pParam; HANDLE hThread; . . . hThread = GetCurrentThread(); SendMessage(pObject, WM_MY_MESSAGE, 0, (LPARAM) hThread); while(1) { //here I read the CommPort . . } return 0; } --In the main Program: LRESULT CMyDlg::MyFunction(WPARAM wParam, LPARAM lParam) { m_hThread = (HANDLE) lParam; //m_hThread is a private HANDLE variable of CMyDlg return 0L; } void CMyDlg::OnWhatEver() { //TerminateThread(m_hThread, -1); //CloseHandle(m_hThread); } If I use TerminateThread((m_hThread, -1) the application disappears but a process is still running which I have to terminate with the Task Manager. If I use CloseHandle(m_hThread) nothing happens... You posted befor that I need simply to exit the thread function - this is exactly what I want to do, but how?
-
Yes, it is a worker thread. In my endless while loop I read the serial port. On an port event I send a message to the main program. I also send a message to the main program with the handle of the thread by using GetCurrentThread() UINT TheThread(LPVOID pParam) { HANDLE* pObject = (HANDLE*)pParam; HANDLE hThread; . . . hThread = GetCurrentThread(); SendMessage(pObject, WM_MY_MESSAGE, 0, (LPARAM) hThread); while(1) { //here I read the CommPort . . } return 0; } --In the main Program: LRESULT CMyDlg::MyFunction(WPARAM wParam, LPARAM lParam) { m_hThread = (HANDLE) lParam; //m_hThread is a private HANDLE variable of CMyDlg return 0L; } void CMyDlg::OnWhatEver() { //TerminateThread(m_hThread, -1); //CloseHandle(m_hThread); } If I use TerminateThread((m_hThread, -1) the application disappears but a process is still running which I have to terminate with the Task Manager. If I use CloseHandle(m_hThread) nothing happens... You posted befor that I need simply to exit the thread function - this is exactly what I want to do, but how?
Vassili wrote: ...I need simply to exit the thread function - this is exactly what I want to do, but how? Change your while(1) loop to have a more meaningful condition. For example:
bool bContinue = true;
while (true == bContinue)
{
// read port
// ...
// if situation warrants, set bContinue = false
}return 0; // this line will cause the thread to exit normally
-
Vassili wrote: ...I need simply to exit the thread function - this is exactly what I want to do, but how? Change your while(1) loop to have a more meaningful condition. For example:
bool bContinue = true;
while (true == bContinue)
{
// read port
// ...
// if situation warrants, set bContinue = false
}return 0; // this line will cause the thread to exit normally
Hi again, within the while loop I use the function ReadFile(...). This means that if there is nothing to read from the serial port the process will not step to the next line. It will remain in the ReadFile-step until a new information will come through the port. Because of this I can't use a condition like bContinue. On the other hand only the main program knows when to end the thread. So the question is: is there a way to terminate my thread from the main program? If not then I will have to redesign my complete program :-(
-
Hi again, within the while loop I use the function ReadFile(...). This means that if there is nothing to read from the serial port the process will not step to the next line. It will remain in the ReadFile-step until a new information will come through the port. Because of this I can't use a condition like bContinue. On the other hand only the main program knows when to end the thread. So the question is: is there a way to terminate my thread from the main program? If not then I will have to redesign my complete program :-(
Is the handle being used by ReadFile() synchronous or asynchronous? By your description, it sounds as though a synchronous handle is being used, thus the blocking in which you describe.
-
Hi again, within the while loop I use the function ReadFile(...). This means that if there is nothing to read from the serial port the process will not step to the next line. It will remain in the ReadFile-step until a new information will come through the port. Because of this I can't use a condition like bContinue. On the other hand only the main program knows when to end the thread. So the question is: is there a way to terminate my thread from the main program? If not then I will have to redesign my complete program :-(