* * *

Author Topic: Delay  (Read 708 times)

Cranky

  • Newbie
  • Posts: 4
Delay
« on: April 26, 2017, 08:07:13 am »
Hello there I need help.
I'm trying my best but I'm stucked. I want to make delay between 2 lines..
Code: Pascal  [Select]
  1. procedure TForm1.Button6Click(Sender: TObject);
  2. begin
  3.    simon[1].Pen.Width:=10;
  4.    sleep(1000);
  5.    simon[1].Pen.Width:=3;
  6. end;
but it always do just line with PenWidth = 3.
Please help me  :)

avra

  • Hero Member
  • *****
  • Posts: 1048
    • Additional info
Re: Delay
« Reply #1 on: April 26, 2017, 08:30:13 am »
That is not a good practice for several reasons, but if you really need it just put Application.ProcessMessages before sleep command.
ct2laz - Easily convert components and projects between Lazarus and CodeTyphon

Cranky

  • Newbie
  • Posts: 4
Re: Delay
« Reply #2 on: April 26, 2017, 08:59:16 am »
Thanks it works fine for me. What I'm doing is simple ''Simon Game''
What other method do you offer me when this is not good practise?

avra

  • Hero Member
  • *****
  • Posts: 1048
    • Additional info
Re: Delay
« Reply #3 on: April 26, 2017, 09:57:43 am »
Your main application thread is blocked. That means user interaction will suffer and become erratic. The best way and your investment for the future is to change your perspective and learn about multithreading:
http://wiki.freepascal.org/Multithreaded_Application_Tutorial

In some situations you can get away with using just simple stuff like this instead of Sleep():
Code: Pascal  [Select]
  1.   procedure ResponsiveSleep(SleepTime: LongInt);
  2.   Var
  3.     FinalTick : QWord;
  4.   begin
  5.     FinalTick := GetTickCount64 + SleepTime;
  6.     while GetTickCount64 < FinalTick do
  7.     begin
  8.       Sleep(10); // optional line to avoid CPU over usage
  9.       // if TickCounter is big then you can here update some timing info form label
  10.       Application.ProcessMessages;
  11.     end;
  12.   end;

but time between ticks is not guaranteed to be the same on all systems. If you need that then initial (low accuracy) calibration would be needed at the start of your application. For more info read the description here:
http://www.freepascal.org/docs-html/rtl/sysutils/gettickcount64.html

In some other situations you can get away with using on demand timers. But for your own good take a look at multithreading.
« Last Edit: April 26, 2017, 10:03:35 am by avra »
ct2laz - Easily convert components and projects between Lazarus and CodeTyphon

Eugene Loza

  • Sr. Member
  • ****
  • Posts: 387
    • Decoherence Studio :)
Re: Delay
« Reply #4 on: April 26, 2017, 10:16:58 am »
My Free and Open Source games in Lazarus/FreePascal/CastleGameEngine:
https://decoherence.itch.io/
(and some ancient games in Turbo Pascal too)

Remy Lebeau

  • Sr. Member
  • ****
  • Posts: 277
    • Lebeau Software
Re: Delay
« Reply #5 on: April 26, 2017, 10:59:25 pm »
Or use timer

Agreed.

Code: [Select]
procedure TForm1.Button6Click(Sender: TObject);
begin
  simon[1].Pen.Width := 10;
  Timer1.Enabled := True;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Timer1.Enabled := False;
  simon[1].Pen.Width := 3;
end;

Or, if you need it to toggle repeatedly:

Code: [Select]
procedure TForm1.Button6Click(Sender: TObject);
begin
  simon[1].Tag := 0;
  Timer1Timer(nil);
  Timer1.Enabled := True;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if simon[1].Tag = 1 then
  begin
    simon[1].Pen.Width := 3;
    simon[1].Tag := 0;
  end else
  begin
    simon[1].Pen.Width := 10;
    simon[1].Tag := 1;
  end;
end;
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) open source project - Admin, Developer

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus