在.net中计时执行时间并忽略操作系统休眠时间
我们正在尝试在我们的应用程序中合并一些代码计时,但遇到了潜在的问题。我们希望计时是稳健的,以便在计算机休眠期间暂停计时,从而为我们提供更精确的活动使用计时。我们已经了解了各种 BCL 和 P/Invoke 方法(Environment.TickCount、DateTime.UtcNow、Stopwatch、QueryPerformanceCounter(我认为 Stopwatch 在内部使用,如果可用)和 timeGetTime(来自 winnm.dll、mmtimer.dll 或 core.dll))但它们都在计算机休眠时继续进行滴答计数。
是否有任何强大的 BCL 或 P/Invoke 调用可以计算实际执行时间并在操作系统休眠时暂停?
我想另一种方法可能是使用上述方法之一并维护一个内部周期性滴答,该滴答可以解释计时中的“漏洞”(这可能表明冬眠期),但这似乎有点麻烦,并且实施和验证时容易出错。
编辑:根据要求,我会添加一些背景。我们正在创建一个小型库来对应用程序中的功能使用进行计时。我们希望避免依赖第三方解决方案,因此我们需要坚持 BCL(或 P/Invoke,尽管我们希望避免这种情况)中的某些内容。我们收到一些反馈,称我们的某些计时偏离了很多,并且发现我们没有意识到我们的计时代码对于操作系统中的休眠功能并不稳健。因此,我们正在寻找一种解决方案来提供程序的实际执行时间。我们不是寻找CPU使用率或类似的较低级别计数器,我们希望有一种方法来测量该功能的实际执行时间,并且我们不将休眠操作系统视为执行时间。
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) and 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用函数
RegisterSuspendResumeNotification
注册系统休眠时的通知:您可能还希望在工作站锁定、屏幕保护程序运行时接收通知,或者桌面使用快速用户切换、使用系统事件通知服务来切换用户:
You can register for notification when the system hibernates, using the function
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: