none
Best way to stop a thread.

    Question

  • Hi

    In multithreading envinronment,

    For stopping a thread i am using the abort(). But it is raising the "threadabortexception."

    May i know what is the good and best way to stop it. (i donot want to make a thread as sleep).

    Thanks in advance.


    NANDAKUMAR.T

    Tuesday, September 9, 2014 5:36 PM

Answers

  • I avoid using Thread.Abort(). Instead, it is better to signal the thread in some way to indicate it is time to clean up and exit. This can be done as simply setting a boolean value that the thread periodically checks, or using one of the other sychronization techniques... such as an event or semaphore. The choice depends on what your thread is expected to do. Only if the thread fails to respond after an interval will I call Thread.Abort().
    Tuesday, September 9, 2014 5:57 PM
  • You should not terminate a thread by calling the Abort() method - this is certainly considered a bad practice - as this may leave your application in an undefined state and provides no opportunity for the cleanup of resources.

    What you should do is to define a volatile variable that the thread that you want to cancel checks periodcally. Please refer to the following page on MSDN for more information and a code sample.

    How to: Create and Terminate Threads (C# Programming Guide): http://msdn.microsoft.com/en-us/library/7a2f3ay4(v=vs.80).aspx

    Tuesday, September 9, 2014 8:44 PM
  • Hello,

    In .NET 4.x+ you can use the Task CancellationToken.


    Cheers,

    Eyal Shilony

    You are free to contact me through 'msdn at shilony net' for anything related to the C# forum.

    Wednesday, September 10, 2014 12:52 AM
    Moderator

All replies

  • Create an override function to the abort (or dispose) to the class which will cleanup all objects.

    jdweng

    Tuesday, September 9, 2014 5:40 PM
  • I avoid using Thread.Abort(). Instead, it is better to signal the thread in some way to indicate it is time to clean up and exit. This can be done as simply setting a boolean value that the thread periodically checks, or using one of the other sychronization techniques... such as an event or semaphore. The choice depends on what your thread is expected to do. Only if the thread fails to respond after an interval will I call Thread.Abort().
    Tuesday, September 9, 2014 5:57 PM
  • You should not terminate a thread by calling the Abort() method - this is certainly considered a bad practice - as this may leave your application in an undefined state and provides no opportunity for the cleanup of resources.

    What you should do is to define a volatile variable that the thread that you want to cancel checks periodcally. Please refer to the following page on MSDN for more information and a code sample.

    How to: Create and Terminate Threads (C# Programming Guide): http://msdn.microsoft.com/en-us/library/7a2f3ay4(v=vs.80).aspx

    Tuesday, September 9, 2014 8:44 PM
  • Hello,

    In .NET 4.x+ you can use the Task CancellationToken.


    Cheers,

    Eyal Shilony

    You are free to contact me through 'msdn at shilony net' for anything related to the C# forum.

    Wednesday, September 10, 2014 12:52 AM
    Moderator