Timing execution time in .net and disregard OS hibernation time
We are trying to incorporate some timing of code in our application and have run into a potential problem. We would like the timing to be robust so that the periods where the computer is hibernating, the timing is paused, giving us a more precise timing of active usage. We have been around various BCL and P/Invoke methods (Environment.TickCount, DateTime.UtcNow, Stopwatch, QueryPerformanceCounter (which I think Stopwatch uses internally if available) an开发者_开发知识库d timeGetTime (from winnm.dll, mmtimer.dll or core.dll)) but all of them continue their tick counting while hibernating the computer.
Are there any robust BCL or P/Invoke calls that will count actual execution time and pause while the operating system is hibernated?
I guess an alternative could be to use one of the above methods and maintain an internal periodic tick that could account for "holes" in the timing (which could indicate period of hibernation), but that seems somewhat cumbersome and error prone to implement and verify.
EDIT: On request, I'll add a little background. We are creating a small library to do timing of feature usage in applications. We would like to avoid taking dependencies on 3rd party solutions so we need to stick to something in the BCL (or P/Invoke, though we would like to avoid that). We have had some feedback that some of our timing was way off and discovered that we had been unaware of the fact that our timing code was not robust towards hibernation in the operating system. Therefor we are looking for a solution to provide actual execution time of the program. We are not looking for CPU usage or similar lower level counters, we would like a way to measure the actual execution time of the feature and we do not consider a hibernating operating system as execution time.
You can register for notification when the system hibernates, using the function RegisterSuspendResumeNotification
:
- https://learn.microsoft.com/en-gb/windows/desktop/api/winuser/nf-winuser-registersuspendresumenotification
You may also want to receive notifications when the workstation is locked, the screensaver runs, or the desktop switches user using Fast User Switching, using System Event Notification Service:
- https://learn.microsoft.com/en-us/windows/desktop/api/sensevts/nf-sensevts-isenslogon-displayunlock
精彩评论