Windows挂起之前的剩余时间
Time left until Windows suspend
有没有办法查询窗口在进入挂起/睡眠模式之前还有多少时间?
我使用的是vbscript,怀疑可能有WMI的答案,但任何语言,如c/c++、*.NET,甚至java,如果可能的话,都可以满足我的需求。
编辑
我希望能够用一个方法查询Windows,而不是在它即将挂起时被事件提醒。
由于Windows将尝试尽快完成进入S3(睡眠)或S4(休眠),因此没有API可以知道剩余的时间。
Windows将向所有进程发送有关挂起的电源状态更改的通知,并允许应用程序为该事件做好准备。
你可以在这里找到你需要的大部分东西。
基本上,您有20秒的时间来处理第一条消息。您的进程可能会延迟回复消息,处理所有各种电源循环任务,例如关闭文件、保存状态等。
您可能需要调用CallNtPowerInformation
API函数,该函数采用以下参数:
NTSTATUS WINAPI CallNtPowerInformation(
_In_ POWER_INFORMATION_LEVEL InformationLevel,
_In_ PVOID lpInputBuffer,
_In_ ULONG nInputBufferSize,
_Out_ PVOID lpOutputBuffer,
_In_ ULONG nOutputBufferSize
);
在InformationLevel
参数中,您传递SystemPowerInformation
枚举值,该值用SYSTEM_POWER_INFORMATION
结构填充lpOutputBuffer
:
typedef struct _SYSTEM_POWER_INFORMATION {
ULONG MaxIdlenessAllowed;
ULONG Idleness;
ULONG TimeRemaining;
UCHAR CoolingMode;
} SYSTEM_POWER_INFORMATION, *PSYSTEM_POWER_INFORMATION;
然后得到以秒为单位的TimeRemaining
。
edit:在VB.NET中测试。编辑:已添加代码。
Queryer.vb
Imports System.Runtime.InteropServices
Public Class Queryer
Const SystemPowerInformation As Integer = 12
Const STATUS_SUCCESS As Integer = 0
Private Structure SYSTEM_POWER_INFORMATION
Public MaxIdlenessAllowed As UInteger
Public Idleness As UInteger
Public TimeRemaining As Integer
Public CoolingMode As Byte
End Structure
<DllImport("PowrProf.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
Public Shared Function CallNtPowerInformation(
ByVal InformationLevel As Int32,
ByVal lpInputBuffer As IntPtr,
ByVal nInputBufferSize As UInt32,
ByVal lpOutputBuffer As IntPtr,
ByRef nOutputBufferSize As UInt32) As UInt32
End Function
Public Function Query() As Integer
Dim PowerInformation As SYSTEM_POWER_INFORMATION
Dim Status As IntPtr = IntPtr.Zero
Dim ReturnValue As UInteger
Try
Status = Marshal.AllocCoTaskMem(Marshal.SizeOf(GetType(SYSTEM_POWER_INFORMATION)))
ReturnValue = CallNtPowerInformation(SystemPowerInformation, Nothing, 0, Status, Marshal.SizeOf(GetType(SYSTEM_POWER_INFORMATION)))
PowerInformation = Marshal.PtrToStructure(Status, GetType(SYSTEM_POWER_INFORMATION))
Catch ex As Exception
Return 0
Finally
Marshal.FreeCoTaskMem(Status)
End Try
Return PowerInformation.TimeRemaining
End Function
End Class
表单:
Public Class Form1
Public Sub Loader() Handles Me.Load
Dim rolex As New Timer()
rolex.Interval = 500
AddHandler rolex.Tick, AddressOf TimerScattato
rolex.Start()
End Sub
Private Sub TimerScattato()
Dim secondi As Integer = q.Query()
Dim iSpan As TimeSpan = TimeSpan.FromSeconds(secondi)
lblTimeLeft.Text = String.Format("{0,2}:{1,2}:{2,2}", iSpan.Hours, iSpan.Minutes, iSpan.Seconds)
End Sub
Private q As New Queryer
End Class
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- 从持续时间构造std::chrono::system_clock::time_point
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- while循环中while循环的时间复杂度是多少
- 使用简单类型列表实现的指数编译时间.为什么
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 时间持续时间到时间字符串
- 使用时间,时间应该如何存储为变量?
- 将带有参考时间的时间戳转换为格式化时间
- 即使在多任务处理时,添加用户时间 + 系统时间(来自 shell 的时间命令)是否是一种可靠的措施?
- 为什么这个经过的时间(帧时间)计算会锁定我的游戏
- 如何使课程持续时间存储时间长度
- 如何格式化标准::时间持续时间
- 不取决于系统时间的时间
- C程序的示例长时间执行时间
- 当(执行时间)>>>(编译时间)时,多个编译单元是否仍然值得?
- 在时间::持续时间类型之间转换,其中两个分母的LCM都很大
- 获取评测函数持续时间的时间(以毫秒为单位)
- 将迭代器返回到多映射中最接近指定时间的时间
- 如何将毫秒添加到自epoch以来的时间持续时间