Killing a Thread within a process.

Posted on 1998-04-14
Last Modified: 2013-12-26
Is there a way to kill a ind. thread within a process
Question by:elcoco
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
  • 2
  • 2

Author Comment

ID: 1292145
Edited text of question

Accepted Solution

nebeker earned 150 total points
ID: 1292146
Depends if you own the thread or not....

DosKillThread() will toast any thread that your process created.  However, it is not exactly recommended because it does not allow the thread to do any cleanup work before it exits.

I also remember some hazards using DosKillThread to toast threads that were created with beginthread instead of DosCreateThread...

The best way is to set some kind of notification flag for the thread, and let it do its cleanup and then exit.

If you are trying to kill an individual thread from another process, then I don't know how to do it.  You might be out of luck....

You also might want to mention *why* you want to kill a thread - there might be a better way of doing it than just destroying the thread...

Expert Comment

ID: 1292147
More info please!
what's an 'ind. thread'?
Are you in the same process?
Do you have the process ID?
Does the thread need to do any cleanup?
Is the thread likley to terminate itsself? (ran into this one, OS/2 reuses thread IDs so if you ask thread N to die, and it does, and another  one starts with thread ID N, and then you kill thread ID N thinking it;s the original one still hanging around, you;re in trouble!)

The quick answer is DosKillThread()

Industry Leaders: 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!


Author Comment

ID: 1292148
thanks, i have an oem package that is creating threads but not cleaning them up. therefore i eventually run out of avail. threads.  the manufact. is slow to resolve this is.  i'm trying to either create a work around or another app. that could close threads that have been inactive. i dont know if this is possible from your answer....would be grateful for any help you can provide

Expert Comment

ID: 1292149
As far as I know, one process cannot kill individual threads from another process.

If you were to shut down the offending process, all of its dead threads would go along with it.  Is there any way you can "restart" the problematic program every once in a while?  (Kind of like the trick used to keep Windows NT servers running - reboot them every night at midnight <g>)

In order for your system to truly run out of available threads, this process must be creating hundreds of them....  How long does it take to run out, and how many threads are in the system when it finally dies?

Expert Comment

ID: 1292150
Another problem qould be determining which of th threads are idle.
Why not just bump up the threads to the max (4095)?
That should keep the app busy chewing them up for a while.


Author Comment

ID: 1292151
Thanks, i have already bumped them up to 4095...the app does open quite a bit of
 threads. its opening an sql table and writing to an ascii file. it will open a thread for each
read AND write. it does this hourly for approx. 40 files with two data pieces a piece and the threads are exhausted after a few days....i can reboot this system,but the problem becomes automating the process. i guess i can figure something out. since i dont have the source code i cant fix and recompile.

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

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: 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…
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

689 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