71 lines
2.1 KiB
C#
71 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Management;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DM_Weight.util
|
|
{
|
|
public class CheckComputerFreeState
|
|
{
|
|
/// <summary>
|
|
/// 创建结构体用于返回捕获时间
|
|
/// </summary>
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
struct LASTINPUTINFO
|
|
{
|
|
/// <summary>
|
|
/// 设置结构体块容量
|
|
/// </summary>
|
|
[MarshalAs(UnmanagedType.U4)]
|
|
public int cbSize;
|
|
|
|
/// <summary>
|
|
/// 抓获的时间
|
|
/// </summary>
|
|
[MarshalAs(UnmanagedType.U4)]
|
|
public uint dwTime;
|
|
}
|
|
|
|
[DllImport("user32.dll")]
|
|
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
|
|
/// <summary>
|
|
/// 获取键盘和鼠标没有操作的时间
|
|
/// </summary>
|
|
/// <returns>用户上次使用系统到现在的时间间隔,单位为秒</returns>
|
|
public static long GetLastInputTime()
|
|
{
|
|
LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
|
|
vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
|
|
if (!GetLastInputInfo(ref vLastInputInfo))
|
|
{
|
|
return 0;
|
|
}
|
|
else
|
|
{
|
|
var count = (Environment.TickCount & Int32.MaxValue) - (long)vLastInputInfo.dwTime;
|
|
var icount = count / 1000;
|
|
return icount;
|
|
}
|
|
}
|
|
|
|
public static ulong GetSystemIdleTime()
|
|
{
|
|
ulong idleTime = 0;
|
|
ManagementClass mc = new ManagementClass("Win32_PerfFormattedData_PerfOS_Processor");
|
|
ManagementObjectCollection moc = mc.GetInstances();
|
|
|
|
foreach (ManagementObject mo in moc)
|
|
{
|
|
idleTime = (ulong)mo.Properties["IdleTime"].Value;
|
|
break;
|
|
}
|
|
Console.WriteLine(idleTime);
|
|
return idleTime;
|
|
}
|
|
|
|
}
|
|
}
|