Solved

How do I interrupt a thread?

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

730 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