Solved

Terminating a Worker Thread

Posted on 1997-04-23
4
724 Views
Last Modified: 2013-11-20
Could you please tell me why the following code will not work :-
CWinThread *hTThread;
TerminateThread(hTThread, 0);
The 'TerminateThread()' function always fails to terminate the thread.
0
Comment
Question by:Bruce Sandeman
  • 2
4 Comments
 

Accepted Solution

by:
dvazquez earned 50 total points
ID: 1301160
Try:
  TerminateThread(hTThread->m_hThread, 0);

You're calling TerminateThread with a pointer to a CWinThread when what it takes is a HANDLE.

0
 
LVL 11

Expert Comment

by:mikeblas
ID: 1301161
While dvazquez's answer is correct, he should have advised you that TerminateThread() is bad news.  You should never, ever, ever ever call TerminateThread().  TerminateThread() is evil incarnate.  You're better off letting your thread hang instead of calling TerminateThread().

TerminateThread() stops a thread dead in its tracks; the thread has no chance to clean up properly, and may actually be interrupted while manipulating system data structures--which can corrupt the heap, blow out GDI, or make USER unstable.

Never, ever, _ever_ call TerminateThread().

Ever.

.B ekiM

0
 

Author Comment

by:Bruce Sandeman
ID: 1301162
ok, so what should I use instead of TerminateThread() ???

0
 
LVL 11

Expert Comment

by:mikeblas
ID: 1301163
You should properly code your thread.  You should write your thread controlling function so that it exits when it needs to.  Maybe this means that you have to check a termination flag or event all over the place. Maybe it means that your thread needs to receive a WM_QUIT message before it can die.

Don't ever call TerminateThread() if you want the users of your software to have a stable system.

.B ekiM


0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
repeatEnd java challenge 42 85
not able to insert into temp table 68 169
canBalance challenge 34 96
Fibonacci challenge 11 132
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question