Solved

How To Kill a Thread completely

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

696 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