Recent

Author Topic: how to use a timer,for some miliseconds  (Read 13859 times)

donnie

  • Jr. Member
  • **
  • Posts: 72
how to use a timer,for some miliseconds
« on: May 06, 2010, 11:41:37 am »
Hello,

Does anyone knows how to use a timer to execute a procedure for some miliseconds?I mean that I want to wait for connecting a port to a PDA.If the port doesn't respond for spesific time I want to end and return false.For example if my procedure is waitcomrespond(); I want to do
Code: [Select]
E:boolean;
while untilmytimerends do
begin
E := waitcomrespond;
end;

if mytimerends E=false
.....

Thank you...
« Last Edit: May 06, 2010, 11:43:52 am by nek »

Zoran

  • Hero Member
  • *****
  • Posts: 1870
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: how to use a timer,for some miliseconds
« Reply #1 on: May 06, 2010, 12:25:55 pm »
This should work (it's simple, without timer component):

Code: [Select]
var
  EndTime: TDateTime;
begin
...
  EndTime := Now + EncodeTime(0, 0, 0, 500); // 0 hours, 0 minutes, 0 seconds, 500 miliseconds - you put what you want.

  While Now < EndTime do
    Application.ProcessMessages; // while this code is waiting, let the other application's code work.

  // Here put the code which will execute after waiting time has passed.  
...
end;
« Last Edit: May 06, 2010, 12:29:00 pm by Zoran »

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: how to use a timer,for some miliseconds
« Reply #2 on: May 06, 2010, 03:04:18 pm »
You can also use EpikTimer, precision timer:

http://wiki.lazarus.freepascal.org/EpikTimer

I've never tested it in Windows CE, but at most it should require minor modifications to work.

cokocool

  • Newbie
  • Posts: 3
Re: how to use a timer,for some miliseconds
« Reply #3 on: May 06, 2013, 11:37:40 pm »
gracias Zoran

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1247
Re: how to use a timer,for some miliseconds
« Reply #4 on: May 07, 2013, 06:45:58 am »
hello kokocool,
the zoran's code is not perfect because it uses too much the cpu if this code is in the main thread. You must add a sleep instruction in the loop to avoid this, like that :
Code: [Select]
While Now < EndTime do
    begin
    Application.ProcessMessages; // while this code is waiting, let the other application's code work.
    Sleep(10);// suspend the main thread for 10 ms between each check on Endtime
   end;

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

 

TinyPortal © 2005-2018