CWinThread Termination
-
How to Terminate a thread created from CWinThread, without checking flag in thread function or ::TerminateThread (WinAPI)? Example: CWinThread* Obj; Obj = AfxBeginThread(ThreadProc, this); UINT ThreadProc(LPVOID *pVoid) { while(1) { //do something } return 0; } Now I want to terminate the ThreadProc without using flag or TeminateThread? Thanks in Adavance :)
-
How to Terminate a thread created from CWinThread, without checking flag in thread function or ::TerminateThread (WinAPI)? Example: CWinThread* Obj; Obj = AfxBeginThread(ThreadProc, this); UINT ThreadProc(LPVOID *pVoid) { while(1) { //do something } return 0; } Now I want to terminate the ThreadProc without using flag or TeminateThread? Thanks in Adavance :)
-
How to Terminate a thread created from CWinThread, without checking flag in thread function or ::TerminateThread (WinAPI)? Example: CWinThread* Obj; Obj = AfxBeginThread(ThreadProc, this); UINT ThreadProc(LPVOID *pVoid) { while(1) { //do something } return 0; } Now I want to terminate the ThreadProc without using flag or TeminateThread? Thanks in Adavance :)
Just exit the thread function using a
return
statement (which is missing but required in your example code). After adding the missingreturn
statement, you may then alsobreak
out of the endlesswhile
loop. -
Just exit the thread function using a
return
statement (which is missing but required in your example code). After adding the missingreturn
statement, you may then alsobreak
out of the endlesswhile
loop.Thank you for your response I want to terminate the ThreadProc from outside of the thread
-
Thank you for your response I want to terminate the ThreadProc from outside of the thread
That was not really clear. Then you have to signal the thread that it should terminate. If you don't want to use a global flag, use some kind of event. To check for events use
WaitForSingleObject
orWaitForMultipleObject
(when multiple events must be handled). Examples can be found in any MFC thread tutorial. An old but very good article is: Using Worker Threads[^] (see the Thread Shutdown Without Polling section). Also available at WorkerThreads[^]. -
How to Terminate a thread created from CWinThread, without checking flag in thread function or ::TerminateThread (WinAPI)? Example: CWinThread* Obj; Obj = AfxBeginThread(ThreadProc, this); UINT ThreadProc(LPVOID *pVoid) { while(1) { //do something } return 0; } Now I want to terminate the ThreadProc without using flag or TeminateThread? Thanks in Adavance :)
Member 13147508 wrote:
How to Terminate a thread created from CWinThread, without checking flag in thread function or ::TerminateThread (WinAPI)? Example: CWinThread* Obj; Obj = AfxBeginThread(ThreadProc, this); UINT ThreadProc(LPVOID *pVoid) { while(1) { //do something } return 0; }
The cleanest way to exit the thread is to return from the thread procedure. And it is exactlz what your ThreadProc does! So, nothing more!
-
Member 13147508 wrote:
How to Terminate a thread created from CWinThread, without checking flag in thread function or ::TerminateThread (WinAPI)? Example: CWinThread* Obj; Obj = AfxBeginThread(ThreadProc, this); UINT ThreadProc(LPVOID *pVoid) { while(1) { //do something } return 0; }
The cleanest way to exit the thread is to return from the thread procedure. And it is exactlz what your ThreadProc does! So, nothing more!
See the OP's response here.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles