Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I interrupt a thread?

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
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.…
Suggested Courses

610 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