Stopping a thread
-
avoid calling TerminateThread [^]
There is no spoon. mailHi Thanx for the link i'll look into it Sujan
-
The thread must finish execution on its own based on some condition. Use WaitForSingleObject or WaitForMultipleObjects if you got wait for some signal and u can also give time. There are other methods also that can be used to stop execution like signalling a flag etc.
Hi Dubey, Thanx 4 ur reply.I'll try to implement it using waitformultipleobjects. Sujan
-
Hi All, I used SuspendThread and ExitInstance to stop a thread created using AfxBeginThread Function. But in some newsgroup messages it is stated that this is not the right method. I've used PostThreadMessage using WM_QUIT, but this is not working. So How to stop a thread created using AfxBeginThread function. Thanx in Advance Sujan
You could create a shutdown event to stop the worker thread execution. Here's an example how to do it:
//In your thread class:
//1. Declare m_eventShutdown variable of the type CEvent.
//2. Add function CYourThreadClass::ShutdownThread { m_eventShutdown.SetEvent();}
//
//In your thread function:
//
int CYourThreadClass::ThreadFunction()
{
while(1)
{
HANDLE h[2];
h[0] = m_eventShutdown.m_hObject;
h[1] = another handle;
DWORD dwRet = ::WaitForMultipleObjects(2,h,FALSE,YOURTIMEOUT)
if(dwRet==WAIT_OBJECT_0)
return 0; //exit thread
else if(dwRet==WAIT_OBJECT_1)
//handle your synchronization object
else if(dwRet==WAIT_TIMEOUT)
//handle timeout
else if(dwRet==WAIT_FAILED)
// handle error
}
}
//
//To terminate the worker thread call CYourThreadClass::ShutdownThread from your UI thread. -
You could create a shutdown event to stop the worker thread execution. Here's an example how to do it:
//In your thread class:
//1. Declare m_eventShutdown variable of the type CEvent.
//2. Add function CYourThreadClass::ShutdownThread { m_eventShutdown.SetEvent();}
//
//In your thread function:
//
int CYourThreadClass::ThreadFunction()
{
while(1)
{
HANDLE h[2];
h[0] = m_eventShutdown.m_hObject;
h[1] = another handle;
DWORD dwRet = ::WaitForMultipleObjects(2,h,FALSE,YOURTIMEOUT)
if(dwRet==WAIT_OBJECT_0)
return 0; //exit thread
else if(dwRet==WAIT_OBJECT_1)
//handle your synchronization object
else if(dwRet==WAIT_TIMEOUT)
//handle timeout
else if(dwRet==WAIT_FAILED)
// handle error
}
}
//
//To terminate the worker thread call CYourThreadClass::ShutdownThread from your UI thread.Hi Markowski Thanx a lot. I am working with it. Sujan
-
Hi All, I used SuspendThread and ExitInstance to stop a thread created using AfxBeginThread Function. But in some newsgroup messages it is stated that this is not the right method. I've used PostThreadMessage using WM_QUIT, but this is not working. So How to stop a thread created using AfxBeginThread function. Thanx in Advance Sujan
Does this article help?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
-
Does this article help?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
Yes Yes Thanx David. Now i am working with it. Sujan
-
You could create a shutdown event to stop the worker thread execution. Here's an example how to do it:
//In your thread class:
//1. Declare m_eventShutdown variable of the type CEvent.
//2. Add function CYourThreadClass::ShutdownThread { m_eventShutdown.SetEvent();}
//
//In your thread function:
//
int CYourThreadClass::ThreadFunction()
{
while(1)
{
HANDLE h[2];
h[0] = m_eventShutdown.m_hObject;
h[1] = another handle;
DWORD dwRet = ::WaitForMultipleObjects(2,h,FALSE,YOURTIMEOUT)
if(dwRet==WAIT_OBJECT_0)
return 0; //exit thread
else if(dwRet==WAIT_OBJECT_1)
//handle your synchronization object
else if(dwRet==WAIT_TIMEOUT)
//handle timeout
else if(dwRet==WAIT_FAILED)
// handle error
}
}
//
//To terminate the worker thread call CYourThreadClass::ShutdownThread from your UI thread.Hi Andrzej One silly question. Sorry. How to manage with WAIT_OBJECT_1.Instead shall i use the created thread handle. I used it and it is always getting into that "Wait Timeout" state. OR have to create another event. Thanx in advance Sujan
-
Hi Andrzej One silly question. Sorry. How to manage with WAIT_OBJECT_1.Instead shall i use the created thread handle. I used it and it is always getting into that "Wait Timeout" state. OR have to create another event. Thanx in advance Sujan
Here's an example which manages with
WAIT_OBJECT_1
. The example demonstates how to print to the debug output every 1 s:// In your thread class:
// Declare m_eventShutdown variable of the type CEvent.
// Add function CYourThreadClass::ShutdownThread { m_eventShutdown.SetEvent();}
// Declare m_eventPrintToDebug variable of the type CEvent.
// Add function CYourThreadClass::PrintToDebug { m_eventPrintToDebug.SetEvent();}
//
//In your thread function:
//
int CYourThreadClass::ThreadFunction()
{
while(1)
{
int nCounter = 0;
HANDLE h[2];
h[0] = m_eventShutdown.m_hObject;
h[1] = m_eventPrintToDebug.m_hObject;
DWORD dwRet = ::WaitForMultipleObjects(2,h,FALSE,INFINITE)
if(dwRet==WAIT_OBJECT_0) // exit thread
return 0;
else if(dwRet==WAIT_OBJECT_1) // print to the debug output
TRACE("Counter = %d\n",n_Counter++);
else if(dwRet==WAIT_TIMEOUT)
// never here if the time-out interval = INFINITE
else if(dwRet==WAIT_FAILED)
// handle error
}
}
//
//In your UI thread call SetTimer(1,1000,NULL) and in OnTimer call CYourThreadClass::PrintToDebug.
//To terminate the worker thread call CYourThreadClass::ShutdownThread from your UI thread. -
Here's an example which manages with
WAIT_OBJECT_1
. The example demonstates how to print to the debug output every 1 s:// In your thread class:
// Declare m_eventShutdown variable of the type CEvent.
// Add function CYourThreadClass::ShutdownThread { m_eventShutdown.SetEvent();}
// Declare m_eventPrintToDebug variable of the type CEvent.
// Add function CYourThreadClass::PrintToDebug { m_eventPrintToDebug.SetEvent();}
//
//In your thread function:
//
int CYourThreadClass::ThreadFunction()
{
while(1)
{
int nCounter = 0;
HANDLE h[2];
h[0] = m_eventShutdown.m_hObject;
h[1] = m_eventPrintToDebug.m_hObject;
DWORD dwRet = ::WaitForMultipleObjects(2,h,FALSE,INFINITE)
if(dwRet==WAIT_OBJECT_0) // exit thread
return 0;
else if(dwRet==WAIT_OBJECT_1) // print to the debug output
TRACE("Counter = %d\n",n_Counter++);
else if(dwRet==WAIT_TIMEOUT)
// never here if the time-out interval = INFINITE
else if(dwRet==WAIT_FAILED)
// handle error
}
}
//
//In your UI thread call SetTimer(1,1000,NULL) and in OnTimer call CYourThreadClass::PrintToDebug.
//To terminate the worker thread call CYourThreadClass::ShutdownThread from your UI thread.Hi Andrzej, Thanx A Lot. I am working with it. Actually i creating a number of threads using AfxBeginThread(..) function and trying to end them. The code goes like this CWinThread *multthreads[MAX_CONNECTIONS]; int count; UINT DataAccessThreading(LPVOID pParam) { CoInitialize(NULL); int* ind= (int *)pParam; int index=*ind; free(ind); while(TRUE) { HANDLE h[2]; h[0] = m_eventShutdown.m_hObject; h[1] = multthreads[count]->m_hThread; DWORD dwRets = (DWORD) multthreads[count]->m_hThread; DWORD dwRet = ::WaitForMultipleObjects(2,h,FALSE,INFINITE); if(dwRet==WAIT_OBJECT_0) { TRACE("THREAD EXIT SUCCESSFULL-Not Killing \n"); return 0; //exit thread } else if(dwRet== (DWORD) multthreads[count]->m_hThread)//handle your synchronization object { test.CacheRefresh(index); Sleep(refreshInterval); //Added by Sujan } else if(dwRet==WAIT_TIMEOUT)//handle timeout { TRACE("Wait Timeout \n"); } else if(dwRet==WAIT_FAILED)// handle error { TRACE("Wait Failed \n"); } } return 0; } and the OnInitDialog i am creating a thread by using int *p=(int *)malloc(sizeof(int)); *p=i; multthreads[i]=AfxBeginThread(DataAccessThreading,p,0,0,NULL); Am i going on the right way. Please comment on this. While using this code i am not getting my event called at all. SORRY FOR DISTURBING YOU. AGAIN Thanx A Lot Sujan
-
Hi Andrzej, Thanx A Lot. I am working with it. Actually i creating a number of threads using AfxBeginThread(..) function and trying to end them. The code goes like this CWinThread *multthreads[MAX_CONNECTIONS]; int count; UINT DataAccessThreading(LPVOID pParam) { CoInitialize(NULL); int* ind= (int *)pParam; int index=*ind; free(ind); while(TRUE) { HANDLE h[2]; h[0] = m_eventShutdown.m_hObject; h[1] = multthreads[count]->m_hThread; DWORD dwRets = (DWORD) multthreads[count]->m_hThread; DWORD dwRet = ::WaitForMultipleObjects(2,h,FALSE,INFINITE); if(dwRet==WAIT_OBJECT_0) { TRACE("THREAD EXIT SUCCESSFULL-Not Killing \n"); return 0; //exit thread } else if(dwRet== (DWORD) multthreads[count]->m_hThread)//handle your synchronization object { test.CacheRefresh(index); Sleep(refreshInterval); //Added by Sujan } else if(dwRet==WAIT_TIMEOUT)//handle timeout { TRACE("Wait Timeout \n"); } else if(dwRet==WAIT_FAILED)// handle error { TRACE("Wait Failed \n"); } } return 0; } and the OnInitDialog i am creating a thread by using int *p=(int *)malloc(sizeof(int)); *p=i; multthreads[i]=AfxBeginThread(DataAccessThreading,p,0,0,NULL); Am i going on the right way. Please comment on this. While using this code i am not getting my event called at all. SORRY FOR DISTURBING YOU. AGAIN Thanx A Lot Sujan
Hi Andrzej, If you have got the problem with my code. I am waiting for your responce. SORRY 4 DISTRUBING U Much Sujan