Solved

Terminating a Worker Thread

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 53
Define unique primary key 9 95
child constructor and parent constructor, overriding and overloading 6 93
mapAB Challlenge 35 174
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.

730 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