Hi Group, (delphi 7 refers)
I haven't used delphi for some time. I have now a very basic app.
Three buttons on form, Start/Stop/Exit

objective>
When Start is pressed, the app enters a while(tick=true) do begin..someCode;...end; What I now require is that, when Stop is pressed, the appl abort thisstart loop and returns to the 'idle' state. I was thinking that, after Start is pressed, now the loop is being excuted, by pressing Stop, the stop proc will make tick=false, thus the Start loop will abort since the while(tick=true) is false.

(exit of course to end appl, this of course i know how to do.... :)

This stop proc does not result in the behaviour which I expected, it continues to stay in the loop. The only way, in delpi ide is to go to Run/Reset.

Else, Do away with Stop-btn, just use Startbtn to Start/End the loop. (The no stopbtn on from)
To omit using the Stop-btn, if it is possible, the StartBtn caption on idle to be eg 'Press to Start' Once the start-btn is pressed, i can change the caption of the start-btn to 'Press to Stop' Now the loop is entered. So, once you press the start btn, the caption changes to 'Press to stop', the app executes the start while(tick=true) do begin.... end; loop. Pressing the start-btn whilst in the loop, the loop to be aborted, start-btn caption reverting to 'Press to Start', system idle?

Is there someone here who can give the correct instruction/method/knowhow in order to exit the
startbutton loop, when Stop is pressed. (How do i know what the 'eventhandler name is?)

I am sure this in rather trivial basic, please help with some code samples or pointers?

Thank you very much.

Regards,
Gert

Hi Pritaeas,
Thank for for your feedback.
Find my code for review.
Thanks,
Gert

unit RVComms_main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, CPort, StdCtrls, Menus;

type
  TRVCommsMain_form = class(TForm)
    btnStart: TButton;
    btnExit: TButton;
    CommPort: TComPort;
    MainMenu1: TMainMenu;
    Config1: TMenuItem;
    About1: TMenuItem;
    btnStop: TButton;
    procedure btnStartClick(Sender: TObject);
    procedure btnStopClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnExitClick(Sender: TObject);
    procedure Config1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  RVCommsMain_form: TRVCommsMain_form;

  tick : boolean;
  comms : boolean;
  done : boolean;
  tx_cntr : integer;

  const chCRLF = #13+#10;
  const chFF   = #12;

implementation

{$R *.dfm}

procedure TRVCommsMain_form.btnStartClick(Sender: TObject);
begin
 tick := true;
   // while (tick=true) do
 //  if (tick=true) then
   while (tick=true) do
     begin
      commPort.WriteStr('01234567890'+chCRLF);
      commPort.WriteStr('0123456789abcdefghijklmnopqrstuvwxyz9876543210ABCDEFGHIJKLMNOPQRSTUVWXYZ'); //72
     end;
end;


procedure TRVCommsMain_form.btnStopClick(Sender: TObject);
begin
 tick := false;
end;

//---------------main---------------------------------------//
//----------------------------------------------------------//
//----------------------------------------------------------//

procedure TRVCommsMain_form.FormCreate(Sender: TObject);

begin
commPort.Open;
comms:=true;
 {
 tick := false;

  if (tick=true) then
 begin
     btnStart.Caption := 'Running';
     comms:=true;
     commPort.Open;

    while (tick=true) do
     begin
      commPort.WriteStr('01234567890'+chCRLF);
      commPort.WriteStr('0123456789abcdefghijklmnopqrstuvwxyz9876543210ABCDEFGHIJKLMNOPQRSTUVWXYZ'); //72
     end;

      commPort.Close;
      comms := false;

     btnStart.Caption := 'Start';

 end;
 }
end;

//------------main end-------------------------------------------//
//---------------------------------------------------------------//

procedure TRVCommsMain_form.btnExitClick(Sender: TObject);
begin
 tick := false;
 if (comms=true) then commPort.Close;
close; 
end;

procedure TRVCommsMain_form.Config1Click(Sender: TObject);
begin
 if (comms=true) then commPort.Close;
 commPort.ShowSetupDialog;
end;

end.

That won't work that way, because the main is single threaded. once the start function runs, none of the others will get called.

Either, use Application.ProcessMessages (ugly fix), or use a TTimer or a TThread.

Thanks, can you give me a example snippet of how to do it?
Gert

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.