Solved

How To Kill a Thread completely

Posted on 2001-06-28
3
361 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
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Decoding 32 bit binary streams 6 42
centeredAverage challenge 8 160
either24  challenge 19 96
Sed question 2 83
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

813 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

15 Experts available now in Live!

Get 1:1 Help Now