Thread problem
-
Hi, I want to ask 1. what is the good instructions to close the Thread? set it to null or using Thread.Abort()? 2. how about aborting the thread? 3. how can I prevent if the thread is in Sleep state, can I also close it or abort it?? I have one class which contains one Thread, if I wnat to re-create a new class, like this.Threadclass = new BThreadClass(); should I close the Thread inside the class again? Thanks for help
-
Hi, I want to ask 1. what is the good instructions to close the Thread? set it to null or using Thread.Abort()? 2. how about aborting the thread? 3. how can I prevent if the thread is in Sleep state, can I also close it or abort it?? I have one class which contains one Thread, if I wnat to re-create a new class, like this.Threadclass = new BThreadClass(); should I close the Thread inside the class again? Thanks for help
Setting the thread to null does not stop it ;) If you don't want to use Thread.Abort, use a
bool
variable:private void MyThreadMethod(){ isRunning = true; while(isRunning){ doSomething(); Sleep(xyz); } }
Set isRunning=false, and the thread stops without an exception. -
Setting the thread to null does not stop it ;) If you don't want to use Thread.Abort, use a
bool
variable:private void MyThreadMethod(){ isRunning = true; while(isRunning){ doSomething(); Sleep(xyz); } }
Set isRunning=false, and the thread stops without an exception.this is true, you have to let a thread stop itself, thread.abord() just raises an exception in the thread to stop it. Problem with this exception is that you cant handle it and the entire app will stop. www.agilis.be
-
this is true, you have to let a thread stop itself, thread.abord() just raises an exception in the thread to stop it. Problem with this exception is that you cant handle it and the entire app will stop. www.agilis.be
You can handle it with the
AppDomain.UnhandledException
or, if applicable,Application.ThreadException
event.Microsoft MVP, Visual C# My Articles