Solved

Terminating a Worker Thread

Posted on 1997-04-23
4
737 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

729 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