How to kill a thread in delphi?

前端 未结 5 1350
慢半拍i
慢半拍i 2020-12-01 05:26

In delphi, a method in TThread is terminate. It seems a subthread can not kill another thread by calling terminate or free. For example A(main form), B (a thread unit), C (a

相关标签:
5条回答
  • 2020-12-01 06:02

    Actually,

    currently most voted answer to this question is incorrect (so as 34 upvoters...) in regard how to forcefully kill a thread.

    You do not use ThreadId as a parameter to TerminateThread procedure. Using ThreadId will cause most likely an "Invalid handle" error or in worse case scenerio - will kill a different thread.

    You should pass a thread handle as a parameter:

    TerminateThread(MyThread.Handle);
    

    More about differences between thread's handle and id can be found here.

    Edit

    Seems @himself corrected his mistake after seeing my answer, so this is no longer relevant.

    0 讨论(0)
  • 2020-12-01 06:02

    Terminate does not kill a thread; it sets the Terminated property to inform the thread that it needs to terminate. It's the thread's responsibility to watch for Terminated and shut itself down gracefully.

    0 讨论(0)
  • 2020-12-01 06:17

    You have to check for Terminate in the thread for this to work. For instance:

    procedure TMyThread.Execute;
    begin
      while not Terminated do begin
        //Here you do a chunk of your work.
        //It's important to have chunks small enough so that "while not Terminated"
        //gets checked often enough.
      end;
      //Here you finalize everything before thread terminates
    end;
    

    With this, you can call

    MyThread.Terminate;
    

    And it'll terminate as soon as it finishes processing another chunk of work. This is called "graceful thread termination" because the thread itself is given a chance to finish any work and prepare for termination.

    There is another method, called 'forced termination'. You can call:

    TerminateThread(MyThread.Handle);
    

    When you do this, Windows forcefully stops any activity in the thread. This does not require checking for "Terminated" in the thread, but potentially can be extremely dangerous, because you're killing thread in the middle of operation. Your application might crash after that.

    That's why you never use TerminateThread until you're absolutely sure you have all the possible consequences figured out. Currently you don't, so use the first method.

    0 讨论(0)
  • 2020-12-01 06:17

    If you might want to terminate a thread then you could be better off spawning another app and killing that if you think its failed - windows will then tidy up after you.

    0 讨论(0)
  • 2020-12-01 06:28

    All the Terminate method does is it sets the Terminated property to true. So you have to manually keep checking that property and then exit the thread method when it is set to true.

    0 讨论(0)
提交回复
热议问题