zoukankan      html  css  js  c++  java
  • 获取系统空闲时间

    //定义结构体
    internal struct LASTINPUTINFO
    {
    public uint cbSize;
    public uint dwTime;
    }

    //引入系统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;
    }

    应用时最好单独开启一个线程来计算  不要使用UI线程

    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 (1000 is to let the result close to 15 s)
    if (idleTicks >= 15)
    {
    this.isRun = false;

    this.Dispatcher.Invoke(new Action(() =>
    {
    this.Close();
    }), null);
    }
    }
    }
    }

  • 相关阅读:
    The Country List
    hdoj1215--七夕节(数学)
    Poj 1654--Area(叉积)
    Poj2229--Sumsets(递推)
    数据预处理 center&scale&box-cox
    caret 分类回归树 用法
    ensemble 的2篇入门 文章
    数组 array 矩阵 list 数据框 dataframe
    R list frame, matrix
    R 如何 隐藏坐标轴
  • 原文地址:https://www.cnblogs.com/luohengstudy/p/4171331.html
Copyright © 2011-2022 走看看