Get System Idle Time

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.