Solved

Terminating a Worker Thread

Posted on 1997-04-23
4
712 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
Comment Utility
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
Comment Utility
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
Comment Utility
ok, so what should I use instead of TerminateThread() ???

0
 
LVL 11

Expert Comment

by:mikeblas
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now