Click here to Skip to main content
15,884,176 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionHow can Terminate AfxbeginThread ? Pin
Le@rner12-Jan-11 18:39
Le@rner12-Jan-11 18:39 
AnswerRe: How can Terminate AfxbeginThread ? [modified] Pin
Andrew Brock12-Jan-11 18:58
Andrew Brock12-Jan-11 18:58 
GeneralRe: How can Terminate AfxbeginThread ? [modified] Pin
Le@rner12-Jan-11 19:12
Le@rner12-Jan-11 19:12 
GeneralRe: How can Terminate AfxbeginThread ? Pin
Andrew Brock12-Jan-11 19:24
Andrew Brock12-Jan-11 19:24 
GeneralRe: How can Terminate AfxbeginThread ? [modified] Pin
Le@rner12-Jan-11 19:26
Le@rner12-Jan-11 19:26 
GeneralRe: How can Terminate AfxbeginThread ? Pin
Andrew Brock12-Jan-11 20:35
Andrew Brock12-Jan-11 20:35 
GeneralRe: How can Terminate AfxbeginThread ? Pin
Le@rner12-Jan-11 20:40
Le@rner12-Jan-11 20:40 
AnswerRe: How can Terminate AfxbeginThread ? [modified] Pin
Andrew Brock12-Jan-11 20:58
Andrew Brock12-Jan-11 20:58 
That code I supplied earlier kills the thread by posting the CM_KILL_THREAD message that I showed you earlier and waits for it to terminate itself.
You may consider making it into a function like
DWORD KillThreadAndWait(CTest_Thread *pThread, DWORD nTimeoutMs = 5000) {
	HANDLE hThread = pThread->m_hThread;
	pThread->PostThreadMessage(CM_KILL_THREAD, 0, 0);
	switch (WaitForSingleObject(hThread, nTimeoutMs)) {
		case WAIT_OBJECT_0:
			TRACE("Thread terminated.\n");
			break;

		case WAIT_TIMEOUT:
			TRACE("Wait for terminate timed out.\n");
			return 0x80000000; //This can be anything that you can identify an error as.

		default:
			TRACE("WaitForSingleObject failed with error %d.\n", GetLastError());
			return 0x80000001; //This can be anything that you can identify an error as.
	}
	//Now that the thread is gone, we can continue, say get the exit code that was passed to ExitThread()
	DWORD nRetCode;
	GetExitCodeThread(hThread, &nRetCode);
	return nRetCode;
}


Then your code from earlier would look like
CTest_Thread *pThread;
pThread = (CTest_Thread *)AfxBeginThread(RUNTIME_CLASS(CTest_Thread), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL);
if (pThread != NULL) {			
	pThread->ResumeThread();
}
TRACE("Thread exited with code 0x%08X\n", KillThreadAndWait(pThread, 5000));


Also, a correction from earlier, instead of using ExitThread, http://msdn.microsoft.com/en-us/library/s3w9x78e(v=vs.80).aspx says "To end the thread, call AfxEndThread from within the thread, or return from the controlling function of the worker thread."

BOOL CTest_Thread::PreTranslateMessage(MSG *pMsg) {
	if (pMsg->message == CM_KILL_THREAD) {
		//CORRECTION: MSDN says to use AfxEndThread when using AfxBeginThread to terminate the thread
		AfxEndThread(0); //0 is the return code, if you need to send a message back to your application
		return TRUE; //I have procesed the message, nothing else needs to
	}
	return FALSE; //get something else to process the message
}


EDIT: Corrected wrong variable name
modified on Thursday, January 13, 2011 3:17 AM

GeneralRe: How can Terminate AfxbeginThread ? Pin
Le@rner12-Jan-11 21:17
Le@rner12-Jan-11 21:17 
AnswerRe: How can Terminate AfxbeginThread ? Pin
Andrew Brock12-Jan-11 21:21
Andrew Brock12-Jan-11 21:21 
GeneralRe: How can Terminate AfxbeginThread ? Pin
Le@rner12-Jan-11 21:30
Le@rner12-Jan-11 21:30 
GeneralRe: How can Terminate AfxbeginThread ? Pin
Andrew Brock12-Jan-11 22:26
Andrew Brock12-Jan-11 22:26 
GeneralRe: How can Terminate AfxbeginThread ? Pin
Le@rner12-Jan-11 23:13
Le@rner12-Jan-11 23:13 
GeneralRe: How can Terminate AfxbeginThread ? Pin
Andrew Brock13-Jan-11 0:11
Andrew Brock13-Jan-11 0:11 
GeneralRe: How can Terminate AfxbeginThread ? Pin
Le@rner12-Jan-11 23:42
Le@rner12-Jan-11 23:42 
GeneralRe: How can Terminate AfxbeginThread ? Pin
Andrew Brock12-Jan-11 23:56
Andrew Brock12-Jan-11 23:56 
GeneralRe: How can Terminate AfxbeginThread ? Pin
Le@rner12-Jan-11 23:59
Le@rner12-Jan-11 23:59 
AnswerRe: How can Terminate AfxbeginThread ? Pin
Prasann Mayekar12-Jan-11 18:59
Prasann Mayekar12-Jan-11 18:59 
AnswerRe: How can Terminate AfxbeginThread ? Pin
Cedric Moonen12-Jan-11 20:49
Cedric Moonen12-Jan-11 20:49 
QuestionWhile Statement Issue. Pin
Mike Certini12-Jan-11 17:53
Mike Certini12-Jan-11 17:53 
AnswerRe: While Statement Issue. Pin
David Crow12-Jan-11 17:57
David Crow12-Jan-11 17:57 
GeneralRe: While Statement Issue. Pin
Andrew Brock12-Jan-11 18:06
Andrew Brock12-Jan-11 18:06 
GeneralRe: While Statement Issue. Pin
Mike Certini12-Jan-11 18:23
Mike Certini12-Jan-11 18:23 
GeneralRe: While Statement Issue. Pin
Andrew Brock12-Jan-11 18:35
Andrew Brock12-Jan-11 18:35 
GeneralRe: While Statement Issue. Pin
Mike Certini12-Jan-11 18:48
Mike Certini12-Jan-11 18:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.