Forum > Beginners

How to use a timer to 'sleep' without 'sleeping'?


OK, I have a few places in my latest projects where I pop up an informational message in a StaticText panel, give the user a few seconds to read it, and pop it back down (basically, resize height... nothing fancy).  Trouble is, for some reason, Sleep messes with the showing of the StaticText, and the window locks up for the duration of the Sleep().  Very annoying.  So now I'm wondering if I can do what I'm envisioning with a TTimer in a procedure, like:  show the message/start timer...timer runs down, hide the message.

So basically, I guess I'm looking for a way to do a one-shot timer.  I've been searching and reading what I can find, but seems timers are more fitted to do timed loops rather than one-shot, self-stopping events, and I'm not really understanding the one-shot implementations I do find.  Anybody have any good example code or links you can point me to?  Thanks.

Hi edvard
Just from the top of my head...:
1) put a TTimer on your form and set the interval to your liking, eg.      1000 = 1 second, so perhaps 5000 is good. (object inspector)
2) set TTimer.Enabled:= false; (object inspector)
3) setup TTimer.OnTimer event (object inspector)

--- Code: ---procedure TfrmMain.Timer1Timer(Sender: TObject);
  Timer1.Enabled:= false;
  // your code to make popup disappear again
--- End code ---
4) in the procedure where you make popup appear:

--- Code: ---begin
  Timer1.Enabled:= true;
  // your code to make popup appear
--- End code ---
Hth - Regards Benny

Hi edvard, here's a qiuck'n'dirty test project for you  8-)
Regards Benny

Awesome, I think I actually understand it now.  Thank you!


[0] Message Index

Go to full version