Uptime On Your App

Share your advanced PureBasic knowledge/code with the community.
Randy Walker
Addict
Addict
Posts: 1008
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Uptime On Your App

Post by Randy Walker »

BarryG wrote: Fri Aug 08, 2025 5:21 am GetTickCount_() doesn't work after 49.7 days of uptime, so it's not usable for this sort of thing. Microsoft says to use GetTickCount64() instead, but that's not immediately available in PureBasic as a single API command. Someone else here will surely step in and show how to use it.
Hi BarryG. once again you are quite correct. And yes someone did share the cetTickCount64() solution so I am posting the sample code here that I saved previously and leave it at that. Enough sample code here now for someone else to put the pieces together if they want it bad enough:

Code: Select all

EnableExplicit

Define Milliseconds
Define H
Define M
Define s

If OpenLibrary(0, "Kernel32.dll")
  Prototype GetTickCount64()
  Define GetTickCount64.GetTickCount64 = GetFunction(0, "GetTickCount64")
  CloseLibrary(0)
EndIf

Milliseconds = GetTickCount64()

H=Milliseconds/3600000
M=Milliseconds/60000-(H*60)
s=Milliseconds/1000-(H*3600+M*60)

Debug GetTickCount64()
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 1008
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Uptime On Your App

Post by Randy Walker »

jacdelad wrote: Fri Aug 08, 2025 8:28 am Also, shouldn't ElapsedMilliseconds() work too (and be cross platform)?
I could be wrong but I think it has the same limitation as GetTickCount_()
Otherwise, yes, You could substitute using ElapsedMilliseconds()
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 1008
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Uptime On Your App

Post by Randy Walker »

BarryG wrote: Fri Aug 08, 2025 3:07 pm Uptime = Time since PC started.
Runtime = How long the app has been running.

That's what my comments are based on.
Simantics Shimantics. Plenty tricks and tips came out of my original post so it's all good. :lol:
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
BarryG
Addict
Addict
Posts: 4155
Joined: Thu Apr 18, 2019 8:17 am

Re: Uptime On Your App

Post by BarryG »

jacdelad wrote: Fri Aug 08, 2025 4:17 pm Just read the first sentence Randy wrote. The uptime if his program. Period.
Okay. My bad.
Randy Walker wrote: Sat Aug 09, 2025 12:14 amI could be wrong but I think it has the same limitation as GetTickCount_()
Otherwise, yes, You could substitute using ElapsedMilliseconds()
ElapsedMilliseconds() is a Quad, so doesn't have the same 49.7 day limit as GetTickCount_(), which is a Word.

You can just use Date() like my previous post shows (viewtopic.php?p=643842#p643842), and avoid "Prototype GetTickCount64()" altogether. Why add extra code that isn't needed? ;)
Randy Walker
Addict
Addict
Posts: 1008
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Uptime On Your App

Post by Randy Walker »

BarryG wrote: Sat Aug 09, 2025 1:40 am
jacdelad wrote: Fri Aug 08, 2025 4:17 pm Just read the first sentence Randy wrote. The uptime if his program. Period.
Okay. My bad.
Randy Walker wrote: Sat Aug 09, 2025 12:14 amI could be wrong but I think it has the same limitation as GetTickCount_()
Otherwise, yes, You could substitute using ElapsedMilliseconds()
ElapsedMilliseconds() is a Quad, so doesn't have the same 49.7 day limit as GetTickCount_(), which is a Word.
Well OK then. Good clarification.
You can just use Date() like my previous post shows (viewtopic.php?p=643842#p643842), and avoid "Prototype GetTickCount64()" altogether. Why add extra code that isn't needed? ;)
You're right! Date() is also a great solution. Tips beget tips. :) Thank again BarrtG!!
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 1008
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Uptime On Your App

Post by Randy Walker »

BarryG wrote: Fri Aug 08, 2025 11:42 am
Randy Walker wrote: Fri Aug 08, 2025 2:55 amI wanted to be able to know how long my app was up and running.
Just use the value of Date() when your app starts, and keep calculating its runtime from that. To get you started:

Code: Select all

start.q=Date()

Repeat
  Sleep_(1000)
  Debug "App runtime is: "+FormatDate("%hh:%ii:%ss",Date()-start)
ForEver
Ok BarryG... Like I said at the start FormatDate failed me same as it does with your code, modified to in iclude %dd

Code: Select all

+FormatDate("%dd/%hh:%ii:%ss",Date()-start)
App runtime is: 01/00:00:01
App runtime is: 01/00:00:02
App runtime is: 01/00:00:03
App runtime is: 01/00:00:04
App runtime is: 01/00:00:05
App runtime is: 01/00:00:06
App runtime is: 01/00:00:07
App runtime is: 01/00:00:08
App runtime is: 01/00:00:09
It still is erroneously adding an extra day.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
User avatar
Demivec
Addict
Addict
Posts: 4265
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Uptime On Your App

Post by Demivec »

Randy Walker wrote: Sat Aug 09, 2025 5:52 pm Ok BarryG... Like I said at the start FormatDate failed me same as it does with your code, modified to in iclude %dd

Code: Select all

+FormatDate("%dd/%hh:%ii:%ss",Date()-start)
App runtime is: 01/00:00:01
App runtime is: 01/00:00:02
App runtime is: 01/00:00:03
App runtime is: 01/00:00:04
App runtime is: 01/00:00:05
App runtime is: 01/00:00:06
App runtime is: 01/00:00:07
App runtime is: 01/00:00:08
App runtime is: 01/00:00:09
It still is erroneously adding an extra day.
Date() Returns an accurate time in seconds that can be used as a start time value and also to calculate the time difference in seconds (timeDifference = currentTime - startTime).

FormatDate() is inadequate for displaying elapsed time because it is tailored for displaying dates. So days range from 1 - 31 and hours from 0 to 23, etc.

You have to use your own routine to display the time elapsed like others have shared earlier in this thread.
User avatar
HeX0R
Addict
Addict
Posts: 1197
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Uptime On Your App

Post by HeX0R »

here is another one

Code: Select all

Procedure$ GetUpTime(Showms = #False)
	Protected ems.q, seconds.q, ms, days, hours, minutes, Result$

	ems     = ElapsedMilliseconds()
	seconds = ems / 1000
	ms      = ems - seconds * 1000
	days    = seconds / 86400
	seconds - days * 86400
	hours   = seconds / 3600
	seconds - hours * 3600
	minutes = seconds / 60
	seconds - minutes * 60
	Result$ = Str(days) + "/" + RSet(Str(hours), 2, "0") + ":" + RSet(Str(minutes), 2, "0") + ":" + RSet(Str(seconds), 2, "0")
	
	If Showms
		Result$ + "." + Left(Str(ms), 1)
	EndIf

	ProcedureReturn Result$
EndProcedure

Procedure TimerEvent()
	SetWindowTitle(0, "Up Time       " + GetUpTime(#True))
EndProcedure

;ElapsedMilliseconds() ;<- call once at the beginning, or just use GetUpTime() regularly, like in this example
OpenWindow(0, 0, 0, 300, 300, "Up Time", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
AddWindowTimer(0, 0, 80)
BindEvent(#PB_Event_Timer, @TimerEvent())


Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Randy Walker
Addict
Addict
Posts: 1008
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Uptime On Your App

Post by Randy Walker »

HeX0R wrote: Sat Aug 09, 2025 8:46 pm here is another one
Uhhh, But for some reason yours is ahowing 4/ days plus the tick up. This wont work either.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 1008
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Uptime On Your App

Post by Randy Walker »

Demivec wrote: Sat Aug 09, 2025 7:46 pm You have to use your own routine to display the time elapsed like others have shared earlier in this thread.
Yes, as I did in my OP.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
User avatar
HeX0R
Addict
Addict
Posts: 1197
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Uptime On Your App

Post by HeX0R »

Randy Walker wrote: Sat Aug 09, 2025 11:04 pm
HeX0R wrote: Sat Aug 09, 2025 8:46 pm here is another one
Uhhh, But for some reason yours is ahowing 4/ days plus the tick up. This wont work either.
The reason is, you are using an older PB version and your PC is since 4 days on.
Randy Walker
Addict
Addict
Posts: 1008
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Uptime On Your App

Post by Randy Walker »

HeX0R wrote: Sun Aug 10, 2025 12:48 am
Randy Walker wrote: Sat Aug 09, 2025 11:04 pm
HeX0R wrote: Sat Aug 09, 2025 8:46 pm here is another one
Uhhh, But for some reason yours is ahowing 4/ days plus the tick up. This wont work either.
The reason is, you are using an older PB version and your PC is since 4 days on.
That's amazing how you deduce that, How? PC was up 4 days+ and I was testing on PB ver5.40
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
User avatar
Demivec
Addict
Addict
Posts: 4265
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Uptime On Your App

Post by Demivec »

Randy Walker wrote: Sun Aug 10, 2025 1:48 am
HeX0R wrote: Sun Aug 10, 2025 12:48 am
Randy Walker wrote: Sat Aug 09, 2025 11:04 pm
HeX0R wrote: Sat Aug 09, 2025 8:46 pm here is another one
Uhhh, But for some reason yours is ahowing 4/ days plus the tick up. This wont work either.
The reason is, you are using an older PB version and your PC is since 4 days on.
That's amazing how you deduce that, How? PC was up 4 days+ and I was testing on PB ver5.40
Hexor is amazing but Sherlock would tell you that the deduction was an elementary process in light of the facts that prior to PureBasic v5.60, ElapsedMilliseconds() returned values based on the uptime of the computer. In v5.60 and later it, ElapsedMilliseconds() returned a value of zero on the first call and the elapsed time on subsequent calls.

Thus Hexor reached the only conclusion that was reachable given the time he posted the code sample and how soon you posted your results that exceed 4 days time. :wink:
Post Reply