PB exe owns system timer resolution of 10000

Just starting out? Need help? Post your questions and find answers here.
HannesH
User
User
Posts: 14
Joined: Fri Dec 09, 2011 5:54 pm

PB exe owns system timer resolution of 10000

Post by HannesH »

I can across a little problem with the system timer resolution.

Whenever I use my little PB exe, it seems to hold the system timer resolution at 1 ms
after it is being set by another application.

When the other application ends (e.g. PB IDE), the system timer resolution persists.
This can be worked out by means of "powercfg -energy". The result of this is the file
"energy-report.html" which tells that the timer resolution was requested by my application.

I do not use functions like timeBeginPeriod() or Nt_SetTimerResolution().

Note: This only occurs when my exe was started with admin rights.

Why would my exe own that timer resolution?
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: PB exe owns system timer resolution of 10000

Post by Lunasole »

HannesH wrote: I do not use functions like timeBeginPeriod() or Nt_SetTimerResolution().
Note: This only occurs when my exe was started with admin rights.
Why would my exe own that timer resolution?
Check what functions your EXE imports, maybe some library affects timer.. nothing to say without sources
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
User avatar
Rings
Moderator
Moderator
Posts: 1427
Joined: Sat Apr 26, 2003 1:11 am

Re: PB exe owns system timer resolution of 10000

Post by Rings »

please post a working sample .
SPAMINATOR NR.1
HannesH
User
User
Posts: 14
Joined: Fri Dec 09, 2011 5:54 pm

Re: PB exe owns system timer resolution of 10000

Post by HannesH »

My goodness, that was as simple as this:

Code: Select all

While #True
  Delay(1000)      ; using this, the system timer resolution is set to 10000 (1 ms) by this application!
  ; Sleep_(1000)   ; using this, the system timer resolution remains whatever it is/was
Wend  
End
Delay() needs to be called only once to obtain this result. So the first call of Delay() will modify the system timer resolution.

I'd understand this to be not a bug but a feature to obtain a higher resolution for Delay(). However, it shall be noted in the documentation.
A timer tool to monitor/set the system timer resolution can be found here: https://vvvv.org/contribution/windows-system-timer-tool
So it seems something related to PB itself.

I simply replaced all Delay() occurrences with Sleep_(), that was it.
User avatar
Rings
Moderator
Moderator
Posts: 1427
Joined: Sat Apr 26, 2003 1:11 am

Re: PB exe owns system timer resolution of 10000

Post by Rings »

well, it was not so simple.
the timertool can only switch between 0,5 and 1 msecs here.
your small code does nothing here with the timer, they remain the same.
note, i also used admin privileges to start timertool and the testcode.
Windows 10, 64 Bit .
anyone else can conform or decline ?

as far as i know, all Delay() commands are apis calls to sleep_() .
maybe i'm wrong, need investigation via fred .
SPAMINATOR NR.1
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: PB exe owns system timer resolution of 10000

Post by cas »

Rings wrote:as far as i know, all Delay() commands are apis calls to sleep_() .
maybe i'm wrong, need investigation via fred .
Delay() does more than simply call Sleep_(). It also internally calls timeBeginPeriod_().

EDIT:
It looks like timeBeginPeriod_() is called only once (on first call to Delay() function). You can revert this behavior with these 2 lines of code at start of your project:

Code: Select all

Delay(0) ;will internally call timeBeginPeriod_(1)
timeEndPeriod_(1); clear previously set minimum timer resolution
This is obviously undocumented feature of Delay() and can change at any time in future PB releases.

EDIT2:
Actually, easiest way (and also future-proof) is to put this at start of your project:

Code: Select all

CompilerIf #PB_Compiler_OS=#PB_OS_Windows
  Macro Delay(ms)
    Sleep_(ms)
  EndMacro
CompilerEndIf
HannesH
User
User
Posts: 14
Joined: Fri Dec 09, 2011 5:54 pm

Re: PB exe owns system timer resolution of 10000

Post by HannesH »

Thanks very much, so my suspicion was correct.

@Rings: If you've not seen timer resolutions other than 0.5/1 ms, you have other applications running and holding
1 ms timer resolution. Before doing the test, you should stop all of those (media player, browser, etc) until you
see a timer resolution > 1. Only than, the test makes sense.

@cas:
It looks like timeBeginPeriod_() is called only once (on first call to Delay() function).

I take that as a result. Can anyone in charge confirm this (and maybe add it to the documentation)?
Post Reply