Jump to content
AaronCatolico1

I'm NOT Able to Stop This Thread....

Recommended Posts

I have a button1 and a label1. When the button1 is clicked, the label1 starts counting up on it's own thread.

The issue is that I have NOT been able to stop the thread when button2 is clicked.

Here's the code I'm working with:

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.Controls.Presentation, FMX.StdCtrls, System.SyncObjs;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private

  public

  end;

{ TMyThread is the class that will handle background work for Label1 }
MyThread1 = class(TThread)
private
    count: Integer;
    msg: string;
     stopThread:boolean;
protected
    procedure Execute; override;
    procedure UpdateUI;
public
    constructor Create;
end;

var
  Form1: TForm1;

implementation

{$R *.fmx}


{ MyThread Constructor }
constructor MyThread1.Create;
begin
  inherited Create(False); // False means it will start right away
  FreeOnTerminate := True; // Free memory when finished
end;


{ Worker Thread for Label1 }
procedure MyThread1.Execute;
var
  i: Integer;
begin
  for i := 1 to 1000 do
  begin

  //Check to see if the was terminated. If so, then exit the for loop.
    if Terminated then
        Exit;

    Sleep(10); // Simulate work (1 second per iteration)
    msg := '# ' + IntToStr(i);
    Synchronize(UpdateUI); // Update UI safely from the main thread
  end;

  Synchronize(UpdateUI);
end;

{ Update the UI for Label1 }
procedure MyThread1.UpdateUI;
begin
  Inc(count);
  Form1.Label1.Text := msg;

  //WHen finished:
  if (count = 1000) then
  begin
     ShowMessage('Thread 1 Finished!');
  end;

end;


{Start the thread when button1 is clicked}
procedure TForm1.Button1Click(Sender: TObject);

begin

  MyThread1.Create; // Create and start the first thread for Label1

end;

procedure TForm1.Button2Click(Sender: TObject);
    //Procedure to stop the MyThread1 thread goes here.

begin

end;

end.

 

Share this post


Link to post

I do not see a call to terminate the thread.

Normally, for me, works like a charm.

procedure TForm1.Button2Click(Sender: TObject);
begin
 MyThread.Terminate;
 MyThread.WaitFor;
 MyThread.Free;
end;

//I don't do this:
FreeOnTerminate := True; // Free memory when finished
procedure TMyThread.Execute;
begin
//if an exception is thrown in the thread we need to catch it
 try
  MyExecute;
 except
  on E:exception do
   exceptionString:=E.Message;
 end;
end;

procedure TMyThread.MyExecute;
begin
 while (not terminated) do
  begin
   try
//some code

   except
   end;

   if not terminated then
    Sleep(sleepAmount);
  end;
end;

 

Edited by Mark-

Share this post


Link to post

I answered your same question on StackOverflow before seeing it posted here too:

 

https://stackoverflow.com/a/79151906/65863

 

I see you actually posted this same question on 3 forums at the same time: Delphi-PRAXiS, StackOverflow, and Lazarus. It's generally considered rude to ask the same question on multiple forums at the same time. It gives people the impression that you are in a hurry and don't value any given forum, and you'll usually get similar answers, and you lessen the value of people duplicating their efforts. Pick a forum, give it some time to amswer (like a few days at least), and if you don't get a satisfactory answer then try another forum if needed. 

Edited by Remy Lebeau
  • Like 3

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×