Jump to content
TreZide

From Help

Recommended Posts

I want to make a form with a "Start" and "Stop" button. Basically hitting "Start" will start a repeating task until "Stop" is hit. How would I go about this? My attempts in the past have lead to issues such as the form freezing, or not starting up. At one point the repeating task didn't even attempt to loop until the form was closed! Any help would be great. Thank you.

Edited by TreZide
Link to comment
Share on other sites

Technically there isn't a really good way of doing this in SCAR, because SCAR isn't capable of multithreading. The best you can do is start a form without locking the script thread and implementing a spinlock in the script to wait for a form action. It isn't a very clean solution though.

Link to comment
Share on other sites

How would I exactly go about doing that, not locking the script when making a form, and leting a loop continue. All I really want is a button to toggle a variable between true and false. The background loop and run forever, the only issue is once the loop is started I'm unable to hit stop.

Link to comment
Share on other sites

Perhaps use a timer?

program TestForm;

type
 TTestForm = record
   MainForm: TForm;
   StartButton: TButton;
   StopButton: TButton;
   Timer: TTimer;
 end;

var
 TestForm: TTestForm;
 StartTime: LongInt;

procedure StartButtonClick(Sender: TObject);
begin
 WriteLn('Start button pressed');
 StartTime := GetSystemTime();
 TestForm.Timer.Enabled := True;
end;

procedure StopButtonClick(Sender: TObject);
begin
 WriteLn('Stop button pressed');
 TestForm.Timer.Enabled := False;
end;

procedure DoTimer(Sender: TObject);
begin
 WriteLn('Elapsed time: ' + IntToStr(GetSystemTime - StartTime)); 
end; 

procedure CreateTestForm;
begin
 TestForm.MainForm := TForm.Create(nil);
 with TestForm.MainForm do
 begin 
   BorderIcons := [biSystemMenu];
   BorderStyle := bsDialog;
   Caption := 'Test Form';
   ClientHeight := 43;
   ClientWidth := 173;
   Position := poOwnerFormCenter;
 end;
 TestForm.StartButton := TButton.Create(TestForm.MainForm);
 with TestForm.StartButton do
 begin
   Parent := TestForm.MainForm;
   Left := 8;
   Top := 8;
   Width := 75;
   Height := 25;
   Caption := 'Start';
   TabOrder := 0;
   OnClick := @StartButtonClick;
 end;
 TestForm.StopButton := TButton.Create(TestForm.MainForm);
 with TestForm.StopButton do
 begin
   Parent := TestForm.MainForm;
   Left := 89;
   Top := 8;
   Width := 75;
   Height := 25;
   Caption := 'Stop';
   TabOrder := 1;
   ModalResult := mrOk;
   OnClick := @StopButtonClick;
 end;
 TestForm.Timer := TTimer.Create(TestForm.MainForm);
 with TestForm.Timer do 
 begin 
   Enabled := False;
   Interval := 1000;
   OnTimer := @DoTimer;
 end;
end; 

procedure ShowTestForm;
begin
 TestForm.MainForm.ShowModal;
end;

procedure FreeTestForm;
begin
 FreeForm(TestForm.MainForm);
 TestForm.MainForm := nil;
end; 

procedure MakeThreadsafeCall(const Proc: String);
var
 V: TVariantArray;
begin
 SetArrayLength(V, 0);
 ThreadSafeCall(Proc, V);
end;

begin
 ClearDebug;
 MakeThreadSafeCall('CreateTestForm');
 MakeThreadSafeCall('ShowTestForm'); 
 MakeThreadSafeCall('FreeTestForm');
end.  // prog

 

Start button pressed

Elapsed time: 1014

Elapsed time: 2028

Elapsed time: 3042

Stop button pressed

Successfully executed (5860.2116 ms)

You can't use TTimer on it's own without an owner form. But since you are using a form anyway this shouldn't be a problem. If you do need to use TTimer on its own simply create a dummy form but never display it.

Edited by Bixby Sayz
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...