Solved

How To Kill a Thread completely

Posted on 2001-06-28
3
356 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
3 Comments
 
LVL 22

Accepted Solution

by:
ambience earned 25 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 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

11 Experts available now in Live!

Get 1:1 Help Now