Recent

Author Topic: slower - Timer  (Read 991 times)

michoux

  • Full Member
  • ***
  • Posts: 113
slower - Timer
« on: September 18, 2023, 07:58:43 pm »
Hello,

I have some simple procedure to show running time, with timer.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Timer1Timer(Sender: TObject);
  2. var H,M,S,S2,MS,s3 : word;
  3.        runningtime:TDateTime;
  4.    begin
  5.       DecodeTime(69214902 /1000/60/60/024, H, M, S, MS);
  6.       runningtime:=Now-EncodeTime(H,M,S,MS);
  7.       label1.Caption:=FormatDateTime('hh.mm.ss.zzz',runningtime);
  8.    end;
  9.  

This will show me running time.
I want to add factor to this running time. for example 0,8, time should run slower for 20 percent ...
Any idea how to make this?

Regards Mich

ASerge

  • Hero Member
  • *****
  • Posts: 2336
Re: slower - Timer
« Reply #1 on: September 18, 2023, 09:18:01 pm »
I want to add factor to this running time. for example 0,8, time should run slower for 20 percent ...
Timer1.Interval := Round(Timer1.Interval * 1.2).

michoux

  • Full Member
  • ***
  • Posts: 113
Re: slower - Timer
« Reply #2 on: September 18, 2023, 09:38:25 pm »
Hmm

This will only calculate the time  slower but the result will be the same at the end.

Code: Pascal  [Select][+][-]
  1. runningtime:=Now-EncodeTime(H,M,S,MS);
  2.  
  3.  

This is the calculation of running time, it can starts from 0 or I take some time from the past when event happened.
I need to I guess somehow add factor to this calculation

TRon

  • Hero Member
  • *****
  • Posts: 3623
Re: slower - Timer
« Reply #3 on: September 18, 2023, 10:29:32 pm »
Pseudo-code:
Code: [Select]
NewTime = Now;
Delta = NewTime - PreviousTime
PreviousTime = newTime
RunningTime = RunningTime + (Delta * 0.8)

Edit: Oh, seem I was sleeping. Though the above does work the following (pseudocode) would be simpler and better:
Code: [Select]
NewTime = now
if startTime is not set
  then StartTime = NewTime
  or in case wanting a runningtime that starts at a certain CustomExpiredTime
  then StartTime = NewTime - CustomExpiredTime
Delta = NewTime - StartTime
RunningTime = Delta * 0.8
« Last Edit: September 19, 2023, 01:50:30 am by TRon »
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

michoux

  • Full Member
  • ***
  • Posts: 113
Re: slower - Timer
« Reply #4 on: September 20, 2023, 11:52:45 am »
Hi,

That sounds nice,

but my runningtime is TDateTime variable.
Can I decode this time only to seconds or miliseconds? With this I could do a factor and then encode it back.

TRon

  • Hero Member
  • *****
  • Posts: 3623
Re: slower - Timer
« Reply #5 on: September 20, 2023, 12:09:12 pm »
... but my runningtime is TDateTime variable.
Can I decode this time only to seconds or miliseconds? With this I could do a factor and then encode it back.
My used variable names are all of type TDateTime.

Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. uses
  4.   {$ifdef unix}
  5.   cthreads,
  6.   {$endif}
  7.   sysutils, fptimer, keyboard;
  8.  
  9. type
  10.   TEvents = object
  11.     procedure OnTimer(Sender: TObject);
  12.   end;
  13.  
  14. procedure TEvents.OnTimer(Sender: TObject);
  15. const
  16.   StartDT : TDateTime = 0.0;
  17. var
  18.   NewDT : TDateTime;
  19.   Delta : TDateTime;
  20.   RealRunningTime : TDateTime;
  21.   FakeRunningTime : TDateTime;
  22. begin
  23. //  writeln('timer event fired');
  24.  
  25.   NewDT := now;
  26.  
  27.   if StartDT = 0.0 then
  28.   begin
  29.     StartDT := NewDT;
  30.     // add additional code here in case you do not want the delta
  31.     // to be 0 on the first run. Subtract the start value from
  32.     // startDT in such case.
  33.   end;
  34.  
  35.   Delta := NewDT - StartDT;
  36.   RealRunningTime := Delta;
  37.   FakeRunningTime := Delta * 0.8;
  38.   write('Real: ', FormatDateTime('hh.mm.ss.zzz', RealRunningTime), '  ');
  39.   write('Fake: ', FormatDateTime('hh.mm.ss.zzz', FakeRunningTime));
  40.   writeln;
  41. end;
  42.  
  43. var
  44.   Events : TEvents;
  45.   Timer  : TFPTimer;
  46.  
  47. procedure TimeWasher;
  48. begin
  49.   Timer := TFPTimer.Create(nil);
  50.   // for non GUI programs (terminal) a thread is required in order
  51.   // for TFPTimer to work
  52.   Timer.UseTimerThread := true;
  53.   Timer.Interval := 1000;
  54.   Timer.OnTimer := @Events.OnTimer;
  55.   Timer.Enabled := true;
  56.  
  57.   writeln('Press enter to stop');
  58.  
  59.   repeat
  60.     sleep(250);
  61.   until keypressed;
  62.  
  63.   Timer.Free;
  64. end;
  65.  
  66. begin
  67.   TimeWasher;
  68. end.
  69.  

This shows the real elapsed time as well as the fake elapsed time which is 20% less than the real time.

This is free pascal code (not Lazarus). You can copy the code in the timer event to your Lazarus form timer event (don't forget to remove the writeln's or run your lazarus app from a shell/terminal). If you use the visual designer to setup TTimer you do not need TPFTimer creation and destruction and the keyboard related stuff used. So oyu can forget everything written in the TimeWasher routine.
« Last Edit: September 20, 2023, 12:15:42 pm by TRon »
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

michoux

  • Full Member
  • ***
  • Posts: 113
Re: slower - Timer
« Reply #6 on: September 20, 2023, 01:09:13 pm »
Thank you very much.
This works perfect.

 

TinyPortal © 2005-2018