Forum > General

TThread - How to stop a thread?

(1/4) > >>

Slawek:
Hello,
How do I properly stop a thread from this example?
https://wiki.freepascal.org/Multithreaded_Application_Tutorial#The_TThread_Class
I have something like this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TMyThread.Execute;var newStatus : string;  i: Integer;  Fin: Boolean;begin  fStatusText := 'TMyThread Starting...';  Synchronize(@ShowStatus);  fStatusText := 'TMyThread Running...';  Fin:=False;  while (not Terminated) and (not Fin) do    begin      //...      for i:= 1 to 9 do begin        newStatus:='Task no '+i.ToString;        sleep(500);        //...        if newStatus <> fStatusText then begin          fStatusText := newStatus;          Synchronize(@Showstatus);        end;      end;      // Terminated := True; // ! But it's read-only !  :(      Fin := True;    end;end; This is probably trivial, but I've never worked with threads before and have no experience.
Would it be correct to use Fin to stop this thread? (row 23)

And how to stop this thread from the outside, e.g. with the Stop button? (if there was no "Fin")
i.e. without "Fin" the task loop will repeat indefinitely. How to stop her?
What method sets the "Terminated" property to True?

Is that correct?

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.btStopClick(Sender: TObject);begin  MyThread.Terminate; //?end;
Please help.

Fred vS:

--- Quote from: Slawek on February 06, 2023, 01:36:49 am ---Is that correct?

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.btStopClick(Sender: TObject);begin  MyThread.Terminate; //?end;
--- End quote ---

Yes.

If you want to only pause the thread you may use a RTLEvent.

Example:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  // global variableevPause: PRTLEvent; // the pointer to a RTLEvent...procedure TMyThread.Execute; // at begin of TMyThread.Execute...  begin      evPause := RTLEventCreate;  // create the RTLEvent...  end; procedure TForm1.btPauseClick(Sender: TObject);  begin   if assigned(evPause) then    begin      RTLeventWaitFor(evPause); // wait for event      RTLeventResetEvent(evPause); // to pause the thread    end;     end;  procedure TForm1.btResumeClick(Sender: TObject);  begin      if assigned(evPause) then      RTLeventSetEvent(evPause); // to resume the thread  end; procedure TForm1.btStopClick(Sender: TObject);   begin    if assigned(MyThread) then      begin       MyThread.Terminate; // if FreeOnTerminate = True else add 'MyThread.free;'       MyThread.WaitFor;       if assigned(evPause) then       RTLeventdestroy(evPause);     end;   end; 

Thaddy:

--- Quote from: Fred vS on February 06, 2023, 01:59:01 am ---
--- Quote from: Slawek on February 06, 2023, 01:36:49 am ---Is that correct?

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.btStopClick(Sender: TObject);begin  MyThread.Terminate; //?end;
Please help.

--- End quote ---

Yes.

--- End quote ---
No.
You forgot MyThread.WaitFor.

Fred vS:

--- Quote from: Thaddy on February 06, 2023, 02:39:06 am ---
--- Quote from: Fred vS on February 06, 2023, 01:59:01 am ---
--- Quote from: Slawek on February 06, 2023, 01:36:49 am ---Is that correct?

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.btStopClick(Sender: TObject);begin  MyThread.Terminate; //?end;
Please help.

--- End quote ---

Yes.

--- End quote ---
No.
You forgot MyThread.WaitFor.

--- End quote ---

Indeed and I forgot RTLeventWaitFor(evPause) for pausing thread (fixed just now).

Roland57:
@Fred

I believe WaitFor should come after Terminate.

Terminate sets the Terminated variable to TRUE, and WaitFor waits until the thread execution is actually terminated.

@Slawek

If you wish your for loop to be executed only one time, you can simply remove the while condition.

Navigation

[0] Message Index

[#] Next page

Go to full version