Solved

How do I interrupt a thread?

Posted on 2004-04-13
4
1,030 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
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
 
LVL 11

Expert Comment

by:calinutz
ID: 10818000
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now