Solved

How To Kill a Thread completely

Posted on 2001-06-28
3
363 Views
Last Modified: 2013-11-20
I have used two worker threads in my project which perform two different operations , i want to terminate the thread completely in the middle of the operation ,how do i use Afxendthread to kill it.
   please explain me with an example, what does that parameter exitcode refer to.

I have for loops running in my thread which does lengthy operation
 say when i press a button kill the thread has to be killed.
i am using worker thread.

even when i am using return statement inside the thread it does no get killed.
 basically i am calling a member function inside the thread function,and in the thread function i have mentioned  if th == NULL return false, wher th is pointing to the dialog.

0
Comment
Question by:vsulu
[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
3 Comments
 
LVL 22

Accepted Solution

by:
ambience earned 25 total points
ID: 6236753
Frankly this not the best way to kill a thread from out-side like this , the only way to kill it from outside is by using TerminateThread, this is dangerous as the thread may not be able to release the committed resources.

AfxEndthread can only be called from inside the thread to end its execution , but its far better to just return from the thread proc and the thread ends automaticaly.

The better way is to signal the thread to kill it self.
0
 
LVL 10

Assisted Solution

by:makerp
makerp earned 25 total points
ID: 6237099
in your thread every so often check an event, call it thread1_stop_event, if it is set exit you thread controlling function. then to stop the thread simply set the event from the mian program then waitforsingleevent on the thread handle. the only problem is if the thread blocks at all, in this case the thread will not end until it un-blocks and loop round then checks the event.

hope this helps
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9490140
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by : ambience, makerp (points to be split)

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Roshan Davis
EE Cleanup Volunteer
0

Featured Post

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
Adapt this command to show who installed 29 112
Separate files macro - Errors on larger data set 18 73
sumHeights2  challenge 7 131
Fibonacci challenge 11 138
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

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