Define Structure body
internal struct Lastinputinfo
{
public UINT cbsize;
public UINT Dwtime;
}
Introducing the System API
[DllImport ("User32.dll")]
private static extern bool Getlastinputinfo (ref lastinputinfo plii);
<summary>
Get the System Idle time
</summary>
<returns></returns>
Public long Getidletick ()
{
Lastinputinfo vlastinputinfo = new Lastinputinfo ();
Vlastinputinfo.cbsize = (UINT) marshal.sizeof (vlastinputinfo);
if (! Getlastinputinfo (ref vlastinputinfo)) return 0;
Return (Environment.tickcount-(long) vlastinputinfo.dwtime)/1000;
}
It is best to open a thread separately to calculate the use of the UI thread
private bool AutoClose = false;
private bool Isrun = true;
Thread monitorsystemfree = null;
Thread monitorsystemfree = new Thread (startautoclose);
Monitorsystemfree.isbackground = true;
Monitorsystemfree.start ();
<summary>
Auto to close window
</summary>
private void Startautoclose ()
{
if (AutoClose)
{
while (This.isrun)
{
Get System Run Ticks
Long idleticks = Getidletick ();
Auto Close time out:15 s (the result close to + s)
if (idleticks >= 15)
{
This.isrun = false;
This. Dispatcher.invoke (new Action () =
{
This. Close ();
}), NULL);
}
}
}
}
Get System Idle Time