查看本文可以获取计算机的空闲时间,然后您将在任意条件下触发事件.
伪代码:
If Computer_is_Idle > 15 minutes Then
Do this
Else
Do that or Wait more...
Run Code Online (Sandbox Code Playgroud)
注意:文章中提供了源代码.
小智 8
谢谢LordCover.这段代码来自这里.该类可以为您控制键盘和鼠标控件.您可以在这样的计时器中使用:
private void timer1_Tick(object sender, EventArgs e)
{
listBox1.Items.Add(Win32.GetIdleTime().ToString());
if (Win32.GetIdleTime() > 60000) // 1 minute
{
textBox1.Text = "SLEEPING NOW";
}
}
Run Code Online (Sandbox Code Playgroud)
控制主要代码.粘贴到您的表单代码.
internal struct LASTINPUTINFO
{
public uint cbSize;
public uint dwTime;
}
public class Win32
{
[DllImport("User32.dll")]
public static extern bool LockWorkStation();
[DllImport("User32.dll")]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
[DllImport("Kernel32.dll")]
private static extern uint GetLastError();
public static uint GetIdleTime()
{
LASTINPUTINFO lastInPut = new LASTINPUTINFO();
lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
GetLastInputInfo(ref lastInPut);
return ((uint)Environment.TickCount - lastInPut.dwTime);
}
public static long GetLastInputTime()
{
LASTINPUTINFO lastInPut = new LASTINPUTINFO();
lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
if (!GetLastInputInfo(ref lastInPut))
{
throw new Exception(GetLastError().ToString());
}
return lastInPut.dwTime;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11873 次 |
| 最近记录: |