Note that there are some explanatory texts on larger screens.

plurals
  1. POSet/Change TThread.FreeOnTerminate while on TThread.OnTerminate
    text
    copied!<p>I've been trying to to set the <code>FreeOnTerminate</code> property <strong>in</strong> the <code>OnTerminate</code> procedure but it seems like it's either too late to set it or it's completely ignoring the <code>write</code> procedure. </p> <p>How can I set/change the <code>FreeOnTerminate</code> property <strong>in</strong> the <code>OnTerminate</code> procedure? Are there any workarounds for that?</p> <p>A little code:</p> <pre><code>unit Unit2; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type TForm2 = class(TForm) Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private procedure OnTestThreadTerminate (Sender : TObject); public { Public declarations } end; type TTestThread = class (TThread) public procedure Execute; override; end; var Form2: TForm2; GlobalThreadTest : TTestThread; implementation {$R *.dfm} procedure TForm2.Button1Click(Sender: TObject); begin GlobalThreadTest := TTestThread.Create (True); GlobalThreadTest.OnTerminate := Self.OnTestThreadTerminate; GlobalThreadTest.FreeOnTerminate := True; GlobalThreadTest.Resume; end; procedure TForm2.Button2Click(Sender: TObject); begin // 2nd Button to try to free the thread... // AFTER BUTTON1 has been clicked! try GlobalThreadTest.Free; except on e : exception do begin MessageBox(Self.Handle, pchar(e.Message), pchar(e.ClassName), 64); end; end; end; procedure TForm2.OnTestThreadTerminate(Sender: TObject); begin TTestThread(Sender).FreeOnTerminate := False; // Avoid freeing... ShowMessage (BoolToStr (TTestThread(Sender).FreeOnTerminate, True)); // FreeOnTerminate Value has been changed successfully! end; procedure TTestThread.Execute; begin // No code needed for test purposes. end; end. </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload