Solved

How do I interrupt a thread?

Posted on 2004-04-13
4
1,046 Views
Last Modified: 2012-06-27
This should be simple... But I cannot get it to work.

This is in Delphi 7 - I've got 2 units, unit1 & unit2 with form1 and form2. Form1 has a single button 'Start'. Form2 has a progress bar and a button 'Cancel'.

Unit1.pas
procedure TForm1.StartClick(Sender: TObject);
var
  i : integer;
begin
  Form2.Show;
  for i := 1 to 1000000 do
     begin
       Form2.ProgressBar1.Position := Form2.ProgressBar1.Position + 1;
       Form2.Update;
     end;
end;

Unit2.pas
procedure TForm2.CancelClick(Sender: TObject);
begin
  Form2.Close;
end;

This is a very simplified example of what I have in my application - on running, Form1 shows - the Start button is clicked which starts a lengthy routine where I need to show the progress - so I show Form2 with a progress bar. What I want to do is be able to give the users the option of cancelling the routine by clicking on the cancel button on Form2. Trouble is that despite Form2 having focus it receives no events - specifically the CancelClick event never gets activated and so I can never escape from the routine.

How do I impliment this correctly? Thanks.
0
Comment
Question by:Bandit999
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 3

Accepted Solution

by:
BJThomsen earned 125 total points
ID: 10816749
You need you application to ProcessMessages during your long process.

procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
begin
  Form2.Show;
  Form2.Canceled := False;
  for i := 1 to 1000000 do
  begin
    if Form2.Canceled then
      Exit;
    Form2.ProgressBar1.Position := Form2.ProgressBar1.Position + 1;
    Form2.Update;
    Application.ProcessMessages;
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
  Canceled := True;
  Form2.Close;
end;

Another way to do this would be to put your long process in an actual thread. Like so:

unit Unit3;

interface

uses
  Classes;

type
  TLongProcess = class(TThread)
  private
    { Private declarations }
  protected
    procedure Execute; override;
    procedure UpdatePosition;
  end;

implementation

{ Important: Methods and properties of objects in visual components can only be
  used in a method called using Synchronize, for example,

      Synchronize(UpdateCaption);

  and UpdateCaption could look like,

    procedure TLongProcess.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }

{ TLongProcess }

uses Unit2;

procedure TLongProcess.Execute;
var
  i: Integer;
begin
  for i := 1 to 1000000 do
  begin
    if Terminated then
      Exit;
    Synchronize(UpdatePosition);
  end;
end;

procedure TLongProcess.UpdatePosition;
begin
  Form2.ProgressBar1.Position := Form2.ProgressBar1.Position + 1;
  Form2.Update;
end;

end.

-----------------------------------------

procedure TForm2.Button1Click(Sender: TObject);
begin
  LongProcess.Terminate;
  Form2.Close;
end;

procedure TForm2.FormShow(Sender: TObject);
begin
  LongProcess := TLongProcess.Create(True);
  LongProcess.FreeOnTerminate := True;
  LongProcess.Resume;
end;

---------------------------------------------------

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2.Show;
end;
0
 
LVL 9

Expert Comment

by:mocarts
ID: 10816756
hi, Bandit :)

there are at least two ways to achieve what you need - repeatedly call Application.ProcessMessages in your processing routine or place your lengthy routine in separate thread and call TThread's Terminate in your Form2.Button1Click method.

wbr, mo.
0
 

Author Comment

by:Bandit999
ID: 10821235
Thanks for the replies - it was the Application.ProcessMessages bit that I was missing. By repeatedly calling that I can now interrupt OK.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question