Page 2 of 2

Re: Geting the system uptime on Windows

Posted: Sun Nov 21, 2010 3:28 pm
by Mistrel
I've rebooted the system several times since then so the number there is way off.

Re: Geting the system uptime on Windows

Posted: Sun Nov 21, 2010 9:50 pm
by PB
I see. That's weird, then, since Microsoft is supplying the info.

Re: Geting the system uptime on Windows

Posted: Thu Dec 09, 2010 8:02 pm
by pcfreak
There was a post once about a bit more tricky way to get the system uptime: http://www.purebasic.fr/english/viewtop ... 12&t=39017
Just do something like

Code: Select all

Procedure.s S2Str(time.q)
 time=time
 d.f=time/86400
 d=Round(d,0)
 h.f=time/3600-(d*24)
 h=Round(h,0)
 m.f=time/60-(d*1440)-(h*60)
 m=Round(m,0)
 s.f=time-(d*86400)-(h*3600)-(m*60)
 s=Round(s,0)
 string.s=""
 If d>0 : string+Str(d)+"days " : EndIf
  If h>0 : string+Str(h)+"houres " : EndIf
   If m>0 : string+Str(m)+"minutes " : EndIf
    If s>0 : string+Str(s)+"seconds" : EndIf
ProcedureReturn string
EndProcedure

Debug S2Str(IntQ(GetInterruptTime() / 10000000))
to see the formatted output.

Re: Geting the system uptime on Windows

Posted: Fri Dec 10, 2010 11:57 pm
by Mistrel
Most impressive! Is this in milliseconds, counting from 0 after a reboot?

Re: Geting the system uptime on Windows

Posted: Sat Dec 25, 2010 11:00 pm
by pcfreak
No, it is in 100 nanoseconds since boot. That's why the division by 10000000 to get to seconds.

Re: Geting the system uptime on Windows

Posted: Sat Jan 01, 2011 10:24 am
by GeBonet
Hi !
First, Happy New Year !
Now I'm no expert, but where is the difference with your procedures and:

Code: Select all

Temp.f=ElapsedMilliseconds()/1000
Debug Temp
H=Int(Temp/3600)
Debug "Heures  : "+Str(H)               ; Nombre d'heures...
M=Int(Temp/60)-H*60
Debug "Minutes : "+Str(M)               ; Nombre de minutes
Debug "----------------------"
I think it's the accuracy? Or what? :wink:
Thank you :!:

Re: Geting the system uptime on Windows

Posted: Sat Jan 01, 2011 3:06 pm
by pcfreak
Well, the second call is later of course, but measured in seconds I get the same result:

Code: Select all

;Public domain, created by Rescator.
;Based on info found at http://www.dcl.hpi.uni-potsdam.de/research/WRK/?p=34
CompilerIf #PB_Compiler_Processor=#PB_Processor_x86
Procedure.q GetInterruptTime()
  !_GetInterruptTime_Repeat_Start:
   !MOV edx,dword [7FFE0008h+4]
   !MOV eax,dword [7FFE0008h]
   !MOV ecx,dword [7FFE0008h+8]
   !CMP edx,ecx
   !JNE _GetInterruptTime_Repeat_Start
  ProcedureReturn
EndProcedure
CompilerElse
Procedure.q GetInterruptTime()
  !MOV qword rdx,7FFE0008h
  !_GetInterruptTime_Repeat_Start:
   !MOVSXD rax,dword [rdx+4]
   !MOVSXD rbx,dword [rdx]
   !MOVSXD rcx,dword [rdx+8]
   !CMP rax,rcx
   !JNE _GetInterruptTime_Repeat_Start
  !SAL rax,32
  !ADD rax,rbx
  ProcedureReturn
EndProcedure
CompilerEndIf

Procedure.s S2Str(time.q)
 time=time
 d.f=time/86400
 d=Round(d,0)
 h.f=time/3600-(d*24)
 h=Round(h,0)
 m.f=time/60-(d*1440)-(h*60)
 m=Round(m,0)
 s.f=time-(d*86400)-(h*3600)-(m*60)
 s=Round(s,0)
 string.s=""
 If d>0 : string+Str(d)+"days " : EndIf
  If h>0 : string+Str(h)+"houres " : EndIf
   If m>0 : string+Str(m)+"minutes " : EndIf
    If s>0 : string+Str(s)+"seconds" : EndIf
ProcedureReturn string
EndProcedure

Debug S2Str(IntQ(GetInterruptTime() / 10000000))
Debug S2Str(IntQ(ElapsedMilliseconds() / 1000))

Re: Geting the system uptime on Windows

Posted: Sat Jan 01, 2011 3:15 pm
by C64
pcfreak wrote:

Code: Select all

Procedure.s S2Str(time.q)
time=time
:?: Time equals time at the start of a procedure? Why?

Re: Geting the system uptime on Windows

Posted: Sat Jan 01, 2011 4:23 pm
by pcfreak
Ups, just ignore that ;)

Re: Geting the system uptime on Windows

Posted: Wed Jun 12, 2013 10:22 am
by em_uk
Here is an adaptaions of PB's way. I use Net statistics workstation to get the up time.

Works pretty much instantly :

Code: Select all

Procedure.s UpTime() ; For XP or higher. Pretty much instant now :) ~ em_uk
  p=RunProgram("cmd.exe","/c net statistics workstation ","",#PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
  If p
    While ProgramRunning(p) : o$+ReadProgramString(p)+#CRLF$ : Wend : CloseProgram(p)
    up$=Trim(Mid(o$,FindString(o$,"Statistics since",1)+16)) : up$=Left(up$,FindString(up$,#CRLF$,1)-1)
  EndIf
  ProcedureReturn up$
EndProcedure

Debug UpTime()

Re: Geting the system uptime on Windows

Posted: Fri Jul 26, 2013 1:52 am
by NoahPhense
Nice..

Re: Geting the system uptime on Windows

Posted: Fri Jul 26, 2013 4:04 am
by jassing
Why shell to cmd 1st? why not just call net.exe ?

Code: Select all

Procedure.s UpTime() ; For XP or higher. Pretty much instant now :) ~ em_uk
  p=RunProgram("net.exe","statistics workstation ","",#PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
  If p
    While ProgramRunning(p) : o$+ReadProgramString(p)+#CRLF$ : Wend : CloseProgram(p)
    up$=Trim(Mid(o$,FindString(o$,"Statistics since",1)+16)) : up$=Left(up$,FindString(up$,#CRLF$,1)-1)
  EndIf
  ProcedureReturn up$
EndProcedure

Debug UpTime()

Re: Geting the system uptime on Windows

Posted: Fri Jul 26, 2013 6:13 pm
by pcfreak
But only works if the system is English.