Recent

Author Topic: Stuck in a wait  (Read 4368 times)

w click

  • Full Member
  • ***
  • Posts: 205
Stuck in a wait
« on: June 24, 2011, 12:43:32 pm »
I've written a procedure that causes the program to wait, but I can't find a way to interrupt it with a key press.
Quote
    while now<waittime do begin
      temptime:=waittime-now;

// Stuff here to update the progressbar and a label showing the time left.   

      if keypressed then waittime:=0;
    end;

Keypressed, from the crt unit, just locks it up completely.  I'm using form1.onkeypress for keyboard handling. I need a sort of 'handle events' instead of a keypressed approach.  Any thoughts would be much appreciated.

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Re: Stuck in a wait
« Reply #1 on: June 24, 2011, 01:11:25 pm »
Why not just handle interface updating from a timer event?

Lengthy processing in events will block other events from occuring

Mando

  • Full Member
  • ***
  • Posts: 181
Re: Stuck in a wait
« Reply #2 on: June 24, 2011, 01:17:15 pm »

 add:
 
Code: [Select]
application.ProcessMessages; in the loop so that the form can respond to messages from the keyboard.
 
 Regards,
 
 

Carver413

  • Full Member
  • ***
  • Posts: 119
Re: Stuck in a wait
« Reply #3 on: June 24, 2011, 01:37:55 pm »
be sure to set keypreview to true;
Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: char);
  private
    { private declarations }
  public
    Timer:Integer;
    Procedure TimeOut;
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  Timeout;
end;

procedure TForm1.FormKeyPress(Sender: TObject; var Key: char);
begin
  Timer:=0;
end;
Procedure TForm1.TimeOut;
begin
  Timer:=30; //set the timer to 30 sec
  while Timer>0 do
  begin
    //your code here
    Form1.Caption:='Timer:'+IntToStr(Timer);
    Application.ProcessMessages; //allow events to be processed
    Sleep(1000); //sleep for one sec
    Dec(Timer);
  end;
  Form1.Caption:='Time out finished';
end;

end.


w click

  • Full Member
  • ***
  • Posts: 205
Re: Stuck in a wait
« Reply #4 on: June 25, 2011, 04:55:05 pm »
Application.processmessages did the trick and, once I'd sorted out my regular issue with getting things to refresh, it all looks good now.  Many thanks.

To be fair to Lasken, that would be intelligent, but I'd have to turn my whole program inside out.

Thanks all.

 

TinyPortal © 2005-2018