amd系统启动时间长
In an interesting question posted here at Experts Exchange, a member asked for a program/script that would perform a reboot if a Windows system has been up for more than a specified amount of time (in his company's case, the IT standard for this is more than 14 days). He also wants an option so that the user can delay the reboot for a specified amount of time (in his company's case, 15 minutes), but a "nag" dialog would continue to be re-displayed until the user finally agrees to a reboot.
在Experts Exchange此处发布的一个有趣的问题中 ,一位成员要求提供一个程序/脚本,如果Windows系统已启动超过指定的时间量,则该程序/脚本将重新启动(对于他的公司而言,此操作的IT标准是超过14天)。 他还希望有一个选项,以便用户可以将重新启动延迟指定的时间(在公司的情况下为15分钟),但是将继续显示“ nag”对话框,直到用户最终同意重新启动为止。 。
Key to a solution for this is the ability to determine how long it has been since a system was started. Fortunately, Windows knows this and makes it available via function calls. One of the function calls is GetTickCount and is documented here:
解决方案的关键是能够确定自系统启动以来已有多长时间。 幸运的是,Windows知道了这一点,并通过函数调用使其可用。 函数调用之一是GetTickCount ,在此处记录:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms724408(v=vs.85).aspx
https://msdn.microsoft.com/zh-CN/library/windows/desktop/ms724408(v=vs.85).aspx
Another function is timeGetTime, documented here:
另一个函数是timeGetTime ,在这里记录:
https://msdn.microsoft.com/en-us/library/windows/desktop/dd757629(v=vs.85).aspx
https://msdn.microsoft.com/zh-CN/library/windows/desktop/dd757629(v=vs.85).aspx
Both of them perform similarly, i.e., returning the elapsed milliseconds since the system was started. Some scripting/programming languages use this capability to make the system uptime available in a built-in variable. For example, the AutoHotkey language has a built-in variable called A_TickCount, which it defines as:
两者的执行方式相似,即返回自系统启动以来经过的毫秒数。 某些脚本/编程语言使用此功能来使系统运行时间在内置变量中可用。 例如, AutoHotkey语言具有一个内置变量A_TickCount ,它定义为:
The number of milliseconds since the computer was rebooted.
自重新启动计算机以来的毫秒数。
Using this built-in variable, here's one line of AutoHotkey code that calculates the system uptime in days:
使用此内置变量,这是一行AutoHotkey代码,可以按天计算系统正常运行时间:
UptimeDays:=(A_TickCount/1000)/(60*60*24)
Well, close, but no cigar! Why? If you read the Microsoft documentation referenced above, you'll see that GetTickCount and timeGetTime both store the value in a DWORD. Because of this, they work for up to 49.7 days only!
好吧,亲密,但没有雪茄! 为什么? 如果您阅读上面引用的Microsoft文档,则会看到GetTickCount和timeGetTime都将值存储在DWORD中。 因此,它们最多只能工作49.7天 !
To overcome this limitation, Windows provides the GetTickCount64 function, documented here:
为了克服此限制,Windows提供了GetTickCount64函数,在此处记录:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms724411(v=vs.85).aspx
https://msdn.microsoft.com/zh-CN/library/windows/desktop/ms724411(v=vs.85).aspx
This returns the elapsed milliseconds since the system was started, but without the 49.7 day limitation.
这将返回自系统启动以来经过的毫秒数,但没有49.7天的限制 。
If your programming/scripting language doesn't provide that as a built-in variable, you should be able to make a DLL call to get it. For example, in AutoHotkey, this will retrieve it:
如果您的编程/脚本语言没有将其作为内置变量提供,则您应该能够进行DLL调用来获取它。 例如,在AutoHotkey中,它将检索它:
TickCount64:=DllCall("Kernel32.dll\GetTickCount64","UInt64")
Here's a working AutoHotkey script that shows everything mentioned above:
这是一个有效的AutoHotkey脚本,显示了上面提到的所有内容:
BuiltInTC:=A_TickCount
GetTC:=DllCall("Kernel32.dll\GetTickCount","UInt")
timeGT:=DllCall("Winmm.dll\timeGetTime","UInt")
GetTC64:=DllCall("Kernel32.dll\GetTickCount64","UInt64")
MsgBox,,Uptime Tests,BuiltInTC=%BuiltInTC%`nGetTC=%GetTC%`ntimeGT=%timeGT%`nGetTC64=%GetTC64%
I ran that script on a WS2012R2 system that had been up for more than 49.7 days. Here's the result:
我在运行了超过49.7天的WS2012R2系统上运行了该脚本。 结果如下:
On the same WS2012R2 system, I then ran another AutoHotkey script that calculates the uptime in days based on both the A_TickCount built-in variable and a call to the GetTickCount64 function. Here's the result:
在同一WS2012R2系统上,然后运行另一个AutoHotkey脚本,该脚本基于A_TickCount内置变量和对GetTickCount64函数的调用来计算以天为单位的正常运行时间。 结果如下:
Problem solved! Note that 87.3-37.6=49.7, which shows that, because of the DWORD limitation, the count essentially starts over at zero every 49.7 days — hence, the need to call GetTickCount64.
问题解决了! 注意87.3-37.6 = 49.7 ,这表明由于DWORD的限制,计数实际上每49.7天从零开始计数-因此,需要调用GetTickCount64 。
Here's a complete, tested AutoHotkey script, based on calling GetTickCount64, that satisfies the requirements specified in the first paragraph of this article:
这是一个完整的,经过测试的AutoHotkey脚本,基于调用GetTickCount64 ,它满足本文第一段中指定的要求:
#Warn,UseUnsetLocal ; warning on uninitialized variables
#NoTrayIcon ; do not show an icon in the system tray
#NoEnv ; avoid checking empty variables to see if they are environment variables
#SingleInstance ignore ; leave old instance running
SetBatchLines,-1 ; run at maximum speed
SetFormat,float,0.1 ; uptime in days to one decimal place
; begin params you may want to change
UptimeDaysLimit:=14
NagTimeMinutes:=30
; end params you may want to change
NagTime:=1000*60*NagTimeMinutes ; param to Sleep is in milliseconds
NagAgain:
UptimeMilliseconds:=DllCall("Kernel32.dll\GetTickCount64","UInt64")
UptimeDays:=(UptimeMilliseconds/1000)/(60*60*24)
If (UptimeDays>UptimeDaysLimit)
{
; MsgBox options (https://autohotkey.com/docs/commands/MsgBox.htm):
; 1 - Buttons are OK and Cancel
; 48 - Icon is exclamation
; 256 - Second button is default (to avoid accidental Enter key causing a reboot)
; 4096 - Dialog is always on top
MsgBox,4401,System Uptime Warning,Your system has been up for %UptimeDays% days`n`nClick OK to reboot now or Cancel to be reminded again in %NagTimeMinutes% minutes
IfMsgBox,OK
{
; Shutdown options (https://autohotkey.com/docs/commands/Shutdown.htm):
; 0 - Logoff
; 1 - Shutdown
; 2 - Reboot
; 4 - Force
; 8 - Power down
Shutdown,2
}
Else
{
Sleep,%NagTime%
Goto,NagAgain
}
}
ExitApp
I commented the code heavily to serve as documentation for it, but if you have questions about how it works, please post here and I'll be happy to try to answer.
我对代码进行了重注释,以用作其文档,但是如果您对它的工作方式有疑问,请在此处发布,我们将很乐意回答。
Sidebar:
侧边栏:
If you're not familiar with AutoHotkey and this article has piqued your interest in it, this other EE article will help you to get started with it:
如果您不熟悉AutoHotkey,并且这篇文章引起了您的兴趣,那么另一篇EE文章将帮助您入门:
AutoHotkey - Getting Started
AutoHotkey-入门
End Sidebar
侧边栏
As a final comment on the issue of determining system uptime, it has been documented that TickCount functions can be inaccurate in Windows 10 because of the Fast Startup feature, which is enabled by default in W10. You may disable it (when logged in as an admin) as follows:
作为确定系统正常运行时间问题的最后评论,据记录,由于快速启动功能(在W10中默认启用),因此TickCount功能在Windows 10中可能不准确。 您可以按以下方式禁用它(以管理员身份登录时):
Control Panel
控制面板
Power Options
电源选项
Choose what the power buttons do
选择电源按钮的功能
Change settings that are currently unavailable
更改当前不可用的设置
You'll see this:
您会看到以下内容:
Un-check the Turn on fast startup (recommended) box, which will be checked by default (if you don't have that box, it means that Hibernation is not enabled).
取消选中“ 打开快速启动(推荐)”复选框,默认情况下将选中该复选框(如果没有该复选框,则表示未启用Hibernate功能)。
If you find this article to be helpful, please click the thumbs-up icon below. This lets me know what is valuable for EE members and provides direction for future articles. Thanks very much! Regards, Joe
如果您发现本文有帮助,请单击下面的大拇指图标。 这使我知道什么对EE成员有价值,并为以后的文章提供了指导。 非常感谢! 问候乔
翻译自: https://www.experts-exchange.com/articles/29533/How-to-solve-the-problem-of-incorrect-System-Uptime-being-reported-when-a-system-has-been-up-for-a-long-time.html
amd系统启动时间长