Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I interrupt a thread?

Posted on 2004-04-13
4
Medium Priority
?
1,055 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 375 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month21 days, 3 hours left to enroll

810 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