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
Solved

How To Kill a Thread completely

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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
Folder Comparison 12 54
sameEnds challenge 3 179
fix34  challenge 9 140
I could not set window to top 4 20
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

840 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