In a program in c# I have 2 threads apart from the main thread. When the user closes the form I want to terminate one of the threads from the main thread. How do I go about doing so?. Please provide me with the code if possible.
If you want to kill off a thread you have started yourself I would suggest holding a reference to it, such as a private field. When the application (or thread) is finishing off you can simply call Thread.Abort()
on that thread.
For example:
private Thread _myWorker;
void doSomething()
{
_myWorker = new Thread(...);
_myWorker.Start();
}
void killWorker()
{
_myWorker.Abort()
}
You should note that then you call Abort()
on the thread it will raise a ThreadAbortException which you should catch within your worker code and handle to cleanup etc. For more details see Thread.Abort
Additionally when your application shuts down its main thread (the message loop, aka Application.Run) the child threads will also be shut down.
Please don't use Thread.Abort
as recommended by the other answers so far, unless you want your program to be in an unknown state (see articles by Ian Griffiths and Chris Sells for more info). If closing the form should actually be killing the app, you're probably okay - but in that case I'd recommend just using background threads anyway, which will automatically die when all foreground threads have terminated.
From Joe Duffy's "Concurrent Programming in Windows":
There are two situations in which thread aborts are always safe:
- The main purpose of thread aborts is to tear down threads during CLR AppDomain unloads. [...]
- Synchronous thread aborts are safe, provided that callers expect an exception to be thrown from the method. [...]
All other uses of thread aborts are questionable at best. [...] While thread aborts are theoretically safer than other thread termination mechanisms, they can still occur at inopportune times, leading to instability and corruption if used without care.
(Synchronous thread aborts are when the thread aborts itself, rather than being aborted by another thread.)
For graceful shutdown (without risking getting into odd states) use a flag which is set periodically from the form and checked from the other threads - taking the memory model into account (e.g. either making the flag volatile or using a lock each time you test or set it). See my article on the topic for an example.
Killing threads from other threads is almost always a bad idea. The correct way to do it is to signal the thread to terminate and then wait for it.
That's because threads should be totally responsible for their own resources as much as practicable, and that includes their lifetime.
It doesn't need to be complicated, a simple variable which can be set by any thread and is read by another thread periodically will do (this isn't really in any language but you should get the idea if you know C):
int exitThread1 = false;
static void Thread1 (void) {
while (!exitThread1) {
// do stuff
}
}
static void mummyProc (void) {
int tid1 = startThread (Thread1);
// dum de dum de dum ...
exitThread1 = true;
joinThread (tid1);
}