Solved

Terminating a Worker Thread

Posted on 1997-04-23
4
720 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 100
Strange date stored 22 123
sumHeights  challenge 17 71
Work with App store 7 52
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 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