CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: stop a thread

  1. #1
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    stop a thread

    Hello everyone,


    About using Abort or Interrrupt to stop a thread, I have two questions.

    1. What are the pros and cons about Abort v.s. Interrupt?

    2. If I program a background thread, compared with normal thread, will there be any special points to take care of (e.g. code in different) if the thread is stopped with Abort (or Interrupt)?


    thanks in advance,
    George

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: stop a thread

    Don't use Abort, signal the thread to exit instead. Research the details in msdn.

  3. #3
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: stop a thread

    Thanks Arjay,


    This is the only issue I found from MSDN about Thread.Abort. Are there any other issues?


    http://msdn2.microsoft.com/en-us/lib...ta(VS.80).aspx

    --------------------
    There is also a chance that a static constructor could be aborted. In rare cases, this might prevent instances of that class from being created in that application domain.
    --------------------

    Quote Originally Posted by Arjay
    Don't use Abort, signal the thread to exit instead. Research the details in msdn.

    regards,
    George

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: stop a thread

    Abort is the .Net equivalent of the Win32 api's TerminateThread or TerminateProcess. In general threads and processes should be signalled to exit rather than terminated; otherwise unpredictable things can happen, resources not released, and so on. Read up on these api's in msdn for more info.

  5. #5
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: stop a thread

    Thanks Arjay,


    You are so knowledgable. But this time I am not caring too much about implementation details about .Net API. :-)

    I am wondering,

    1. Why Thread.Abort will cause resource leak (e.g. expected finally block or Finalizer is not executed)?

    2. How do we avoid such leak?

    Quote Originally Posted by Arjay
    Abort is the .Net equivalent of the Win32 api's TerminateThread or TerminateProcess. In general threads and processes should be signalled to exit rather than terminated; otherwise unpredictable things can happen, resources not released, and so on. Read up on these api's in msdn for more info.

    regards,
    George

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: stop a thread

    George, if you can't find the answer in msdn, you need to roll up your sleeves, write some code, and test your ideas out. With a couple of Console.Write or Debug.Write statements you can find out quick enough.

    As far as preventing a resource leak when using Abort, the answer is simple - don't use Abort (or TerminateThread or TerminateProcess). The Msdn documentation pretty much tells you this.

  7. #7
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: stop a thread

    Thanks Arjay, :-)


    How about Thread.Interrupt, also have some resource leak issues? :-)

    In my point of view, if we handle ThreadInterruptException, release resource, then it should be fine and no resource leak.

    Quote Originally Posted by Arjay
    George, if you can't find the answer in msdn, you need to roll up your sleeves, write some code, and test your ideas out. With a couple of Console.Write or Debug.Write statements you can find out quick enough.

    As far as preventing a resource leak when using Abort, the answer is simple - don't use Abort (or TerminateThread or TerminateProcess). The Msdn documentation pretty much tells you this.

    regards,
    George

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: stop a thread

    Quote Originally Posted by George2
    Thanks Arjay, :-)


    How about Thread.Interrupt, also have some resource leak issues? :-)

    In my point of view, if we handle ThreadInterruptException, release resource, then it should be fine and no resource leak.




    regards,
    George
    See my previous post about rolling up your sleeves, writing some code, and trying it out.

    Oh, and what's wrong with everyone's suggestion of just signalling the thread to exit?

  9. #9
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: stop a thread

    Thanks Arjay,


    My thread is doing some time consuming calculations, how could I receive signal in the midddle? :-)

    My idea is, if I have to use signal, I need to check in the middle of calculations, and using the timeout form of WaitOne. But my concern is it will degrate the performance of my calculation (my concern is wait signal timeout). Any suggestions?

    Quote Originally Posted by Arjay
    See my previous post about rolling up your sleeves, writing some code, and trying it out.

    Oh, and what's wrong with everyone's suggestion of just signalling the thread to exit?

    regards,
    George

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: stop a thread

    Quote Originally Posted by George2
    Any suggestions?
    Yes. Write it with an event and check the event as often as necessary within the thread.

    Then profile the application and MEASURE the difference in performance.

  11. #11
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: stop a thread

    Thanks Arjay,


    1.

    If I set the event in signalled status, then next time, when the thread checks the singal by using WaitOne, it will be blocked, right?

    2.

    You mean using the timeout form of wait to check event, like this one?

    http://msdn2.microsoft.com/en-us/lib...t0(VS.80).aspx

    Quote Originally Posted by Arjay
    Yes. Write it with an event and check the event as often as necessary within the thread.

    Then profile the application and MEASURE the difference in performance.

    regards,
    George

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: stop a thread

    Yes, use a timeout of zero to quickly determine if the event has been set.

  13. #13
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: stop a thread

    Thanks Arjay,


    I am writing code and I have some issues dealing with the exitContext parameter of WaitOne, do you have any ideas?

    http://www.codeguru.com/forum/showthread.php?t=451479

    Quote Originally Posted by Arjay
    Yes, use a timeout of zero to quickly determine if the event has been set.

    regards,
    George

  14. #14
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: stop a thread

    George, please don't cross post.

    It is my only hope that one day we will respond to a post without getting asked yet one more followup question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured