C#オペレーティングシステムが空いているかどうかを検出し、システムが空いていることを実現した後、いくつかの操作を行う
5664 ワード
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 - (long)vLastInputInfo.dwTime;
var icount = count / 1000;
return icount;
}
}
}
呼び出し:
static void Main(string[] args)
{
Timer t = null;
t = new Timer((o) =>
{
var result = GetLastInputTime();
Console.WriteLine(result);
if (result < 1*60) return;
if (t == null) return;
t.Dispose();
Console.WriteLine(" 1 !");
}, null, 1000, 1000);
Console.ReadLine();
}