TreZide Posted March 15, 2014 Share Posted March 15, 2014 (edited) 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 March 15, 2014 by TreZide Quote Link to comment Share on other sites More sharing options...
FHannes Posted March 29, 2014 Share Posted March 29, 2014 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. Quote Link to comment Share on other sites More sharing options...
TreZide Posted April 1, 2014 Author Share Posted April 1, 2014 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. Quote Link to comment Share on other sites More sharing options...
Bixby Sayz Posted April 3, 2014 Share Posted April 3, 2014 (edited) 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 pressedElapsed 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 April 3, 2014 by Bixby Sayz Quote Link to comment Share on other sites More sharing options...