Recent

Author Topic: Proper way to terminate a thread?  (Read 3521 times)

alpine

  • Hero Member
  • *****
  • Posts: 1243
Re: Proper way to terminate a thread?
« Reply #30 on: June 05, 2024, 03:13:34 pm »
Just to make sure we're talking about the same thing: I'm speaking about TThread.Finished, not TThread.Terminated.
You're right. Finished is just what I've meant by 'Completed'. It goes True just before FreeOnTerminate check in ThreadFunc. No need to introduce anything new.
Sorry, my bad.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

rvk

  • Hero Member
  • *****
  • Posts: 6324
Re: Proper way to terminate a thread?
« Reply #31 on: June 06, 2024, 11:26:15 am »
BTW. What's wrong with using TThread.WaitFor on a terminated, but net yet freed TThread ??

This works for me:

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyThread = class(TThread)
  3.   private
  4.   protected
  5.     procedure Execute; override;
  6.   end;
  7.  
  8. procedure TMyThread.Execute;
  9. begin
  10. end;
  11.  
  12. //...
  13. var
  14.   T: TMyThread;
  15. begin
  16.   T := TMyThread.Create(false);
  17.   Sleep(2000);
  18.   T.Terminate;
  19.   T.WaitFor; // even though there is no Execute (or has finished) WaitFor should still work but passthrough
  20. end;

So the original problem, where WaitFor is a problem when the thread has already terminated, but not freed, lies somewhere else.

alpine

  • Hero Member
  • *****
  • Posts: 1243
Re: Proper way to terminate a thread?
« Reply #32 on: June 06, 2024, 01:41:49 pm »
BTW. What's wrong with using TThread.WaitFor on a terminated, but net yet freed TThread ??
Nothing wrong! Otherwise it will be almost useless ...
As a matter of fact it works from the very moment of TThread.Create until finally freed.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Thaddy

  • Hero Member
  • *****
  • Posts: 15526
  • Censorship about opinions does not belong here.
Re: Proper way to terminate a thread?
« Reply #33 on: June 06, 2024, 03:28:50 pm »
Indeed.
My great hero has found the key to the highway. Rest in peace John Mayall.
Playing: "Broken Wings" in your honour. As well as taking out some mouth organs.

 

TinyPortal © 2005-2018