ElapsedMilliseconds( )

Just starting out? Need help? Post your questions and find answers here.
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: ElapsedMilliseconds( )

Post by mestnyi »

What you want to tell me, I can't understand. did you run my code on windows?
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: ElapsedMilliseconds( )

Post by mk-soft »

The DoubleClickTime() function returns the system setting.
Not the last pressed time. But I think that is clear.

Under windows, it returns 500ms, which is correct.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: ElapsedMilliseconds( )

Post by mestnyi »

mk-soft wrote: Mon Feb 06, 2023 5:58 pm The DoubleClickTime() function returns the system setting.
This is understandable to this function, I have no questions, why are you sticking to it? I have a question for another function, is it really hard to understand?

Code: Select all

Procedure ClickCount( )
  Static ClickTime.q, ClickCount
  Protected DoubleClickTime = 500
  
  If ClickTime And
     DoubleClickTime > ( ElapsedMilliseconds( ) - ClickTime )
    ClickCount + 1
  Else
    ClickCount = 1
  EndIf
  ClickTime = ElapsedMilliseconds( )
  
  ProcedureReturn ClickCount
EndProcedure

Debug ClickCount( )
Delay(498)
Debug ClickCount( )
Delay(498)
Debug ClickCount( )
Delay(498)
Debug ClickCount( )
on the Mac os returns 1;2;3;4
on the windows returns 1;1;2;3

this is because the first time ElapsedMilliseconds( ) returns 0
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: ElapsedMilliseconds( )

Post by mk-soft »

...
Last edited by mk-soft on Mon Feb 06, 2023 7:21 pm, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: ElapsedMilliseconds( )

Post by mk-soft »

mestnyi wrote: Mon Feb 06, 2023 6:46 pm
mk-soft wrote: Mon Feb 06, 2023 5:58 pm The DoubleClickTime() function returns the system setting.
This is understandable to this function, I have no questions, why are you sticking to it? I have a question for another function, is it really hard to understand? on the Mac os returns 1;2;3;4
on the windows returns 1;1;2;3

this is because the first time ElapsedMilliseconds( ) returns 0
Window and macOS start time is minimal different

Code: Select all


Procedure ClickCount( )
  Static ClickTime.q, ClickCount
  Protected DoubleClickTime = 500
  
  If ClickTime And
     DoubleClickTime > ( ElapsedMilliseconds( ) - ClickTime )
    ClickCount + 1
  Else
    ClickCount = 1
  EndIf
  ClickTime = ElapsedMilliseconds( )
  
  ProcedureReturn ClickCount
EndProcedure

;Init
ElapsedMilliseconds()
Delay(100)

Debug ClickCount( )
Delay(498)
Debug ClickCount( )
Delay(498)
Debug ClickCount( )
Delay(498)
Debug ClickCount( )
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: ElapsedMilliseconds( )

Post by mestnyi »

Maybe that way you will understand what I am trying to convey to you.

Code: Select all

Procedure ClickCount( )
  Static ClickTime.q, ClickCount
  Protected DoubleClickTime = 500
  
  Debug " time "+  Str((ElapsedMilliseconds( )-ClickTime) + Bool( #PB_Compiler_OS ) * 492)
  
  If DoubleClickTime > ( ElapsedMilliseconds( ) - ClickTime ) + Bool( #PB_Compiler_OS ) * 492
    ClickCount + 1
  Else
    ClickCount = 1
  EndIf
  ClickTime = ElapsedMilliseconds( )
  
  ProcedureReturn ClickCount
EndProcedure

Debug ClickCount( )
Delay(498)
Debug ClickCount( )
Delay(498)
Debug ClickCount( )
Delay(498)
Debug ClickCount( )

; windows
; time 0
; 1
;  time 9
; 2
;  time 9
; 3
;  time 15
; 4

; mac os
;  time 695
; 1
;  time 498
; 2
;  time 498
; 3
;  time 499
; 4
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: ElapsedMilliseconds( )

Post by mk-soft »

There seems to be a delay in macOS when ElapseMilliseconds is called for the first time. Something from 16 to 1000 ms.
With windows this is always zero.

Thus, the difference calculation is not correct on the first run.
Therefore, one must (as before) save the time on the first run.

Update

Code: Select all

Procedure ClickCount( )
  Static ClickTime.q, LastClickTime, ClickCount
  Protected DoubleClickTime = 500
  
  If Not LastClickTime
    LastClickTime = ElapsedMilliseconds()
  EndIf
  
  
  ClickTime = ElapsedMilliseconds()
  Debug " time " + ClickTime  
  
  If (ClickTime - LastClickTime) < DoubleClickTime
    ClickCount + 1
  Else
    ClickCount = 1
  EndIf
  LastClickTime = ClickTime
  
  ProcedureReturn ClickCount
EndProcedure

Debug ClickCount( )
Delay(496)
Debug ClickCount( )
Delay(496)
Debug ClickCount( )
Delay(496)
Debug ClickCount( )
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: ElapsedMilliseconds( )

Post by mestnyi »

Code: Select all

  StartTime.q = ElapsedMilliseconds()     ; Get the actual value
  Delay(1000)                             ; Wait 1000 milliseconds
  Debug ElapsedMilliseconds() - StartTime ; Displayed value should be about 1000 milliseconds
on the windows, this code returns 25
That's where the error is.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: ElapsedMilliseconds( )

Post by mk-soft »

Can't be ...

Your PB or OS is broken. It must come out 1000 ms.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: ElapsedMilliseconds( )

Post by mestnyi »

mk-soft wrote: Mon Feb 06, 2023 8:27 pm Can't be ...

Your PB or OS is broken. It must come out 1000 ms.
here I am about it.
I have Windows XP Virtualbox and purebasic 573 x86 lts.
I can not get purebasic x64 to work writes "the compiler isn't loaded yet... Please try again"
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: ElapsedMilliseconds( )

Post by BarryG »

VirtualBox is likely the problem. My own app has issues with VirtualBox, too, but it runs fine on any real PC that I test it on.
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: ElapsedMilliseconds( )

Post by mestnyi »

But what about the elapsedMilliseconds() under the hood?
Or how to get it using the API?
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: ElapsedMilliseconds( )

Post by Olli »

You never should call ElapsedMilliseconds() any times.

-> Synchroneous system = one time
-> Asynchroneous system (clock analyzing) = any time

Use Global Lap = ElapsedMilliseconds() in your main procedure.

Code: Select all

Procedure ClickCount( )
  Static ClickTime.q
  Static ClickCount.i
  
  Protected DoubleClickTime = 500
  Protected Lap = ElapsedMilliseconds()
  
  If ClickTime And
     DoubleClickTime > ( Lap - ClickTime )
    ClickCount + 1
  Else
    ClickCount = 1
  EndIf
  ClickTime = Lap
  
  ProcedureReturn ClickCount
EndProcedure
Even in a normal way, this is bad, because you clock measurement depends of the ClickCount() call frequency, but not, of the clicking frequency.

Code: Select all

Procedure ClickCount(Reset = 0)
    Protected Lap.q = ElapsedMilliseconds()
    Static initialTime.q
    Static finalTime.q
    Protected deltaTime.q
    Static clickCount.i
    initialTime = finalTime
    deltaTime = finalTime - initialTime
    If deltaTime > 30 And deltaTime < 300
     clickCount + 1
    EndIf
    If Reset
     clickCount = 0
    EndIf
    ProcedureReturn clickCount
EndProcedure
That is the reason you should add a sync timer.

Else, there is this :

Code: Select all

If WindowEvent() = #PB_Event_LeftDoubleClick
 Debug "double click"
EndIf
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: ElapsedMilliseconds( )

Post by Oso »

BarryG wrote: Mon Feb 06, 2023 10:08 pm VirtualBox is likely the problem. My own app has issues with VirtualBox, too, but it runs fine on any real PC that I test it on.
Just for the record, the example code works perfectly on my system which runs under VirtualBox inside a Linux server that I connect to through RDP. I don't normally experience any problems with VirtualBox, at least not that I'm aware of :D I see precisely 1,000ms from the below.

Code: Select all

  StartTime.q = ElapsedMilliseconds()     ; Get the actual value
  Delay(1000)                             ; Wait 1000 milliseconds
  Debug ElapsedMilliseconds() - StartTime ; Displayed value should be about 1000 milliseconds
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: ElapsedMilliseconds( )

Post by Olli »

@Oso
mestny wrote:I have Windows XP Virtualbox[...]
XP OS doesn't allow the processus from having an accurate duration :
Delay(1 to 16) = waits 16ms, 17ms or more...
Delay(17) = waits 32ms, 33ms, 34ms or more...
etc...

And ElapsedMilliseconds() gives this :
0, 0, 0, 0
16, 16, 16, 16, 16,
33, 33, 33, 33, 33,
48, 48, 48,
49, 49, 49, 49, 49,
50,
65, 65, 65, 65, etc...
Post Reply