如题。我的目的是自己做一个关机软件。
判断关机的条件是:
1。CPU占用率长期稳定于一个低水平,内存也一样
2。鼠标键盘长时间没有操作市面上有一款【自动关机3000】的,只能用在一台机器上,所以想自己搞搞,咋入手呢?

解决方案 »

  1.   

    1. 查看CPU使用率,msdn上有下载
    2. 检查空闲时间
      

  2.   

    我找到这么段代码(窗体上放了一个timer控件),但是运行是没有窗口没有任何反应,环境是XP+vs2005,是哪里出问题呢?
    [code]
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct LASTINPUTINFO
            {
                [MarshalAs(UnmanagedType.U4)]
                public int cbSize;
                [MarshalAs(UnmanagedType.U4)]
                public uint dwTime;
            }        [DllImport("user32.dll")]
            public static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);        public static long GetLastInputTime()
            {
                LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
                vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
                if (!GetLastInputInfo(ref vLastInputInfo)) return 0;
                return Environment.TickCount - (long)vLastInputInfo.dwTime;
            }        public long getIdleTick()
            {
                LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
                vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
                if (!GetLastInputInfo(ref    vLastInputInfo)) return 0;
                return Environment.TickCount - (long)vLastInputInfo.dwTime;
            }         private void Form1_Load(object sender, EventArgs e)
            {
                this.timer1.Enabled = true;
                this.timer1.Interval = 2000;
            }
            private void timer1_Tick(object sender, EventArgs e)
            {
                long i = getIdleTick();
                this.Text = string.Format(" jinjazz说:您已经{0}ms没有动了 ", i);
                if (i > 2 * 1000)
                {
                    this.WindowState = FormWindowState.Minimized;
                }
                else
                {
                    this.WindowState = FormWindowState.Normal;
                } 
                //label1.Text = string.Format("用户已经{0}秒没有路过了", GetLastInputTime() / 1000);
            } 
        }
    }
    [/code]
      

  3.   

    代码如下
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct LASTINPUTINFO
            {
                [MarshalAs(UnmanagedType.U4)]
                public int cbSize;
                [MarshalAs(UnmanagedType.U4)]
                public uint dwTime;
            }        [DllImport("user32.dll")]
            public static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);        public static long GetLastInputTime()
            {
                LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
                vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
                if (!GetLastInputInfo(ref vLastInputInfo)) return 0;
                return Environment.TickCount - (long)vLastInputInfo.dwTime;
            }        public long getIdleTick()
            {
                LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
                vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
                if (!GetLastInputInfo(ref    vLastInputInfo)) return 0;
                return Environment.TickCount - (long)vLastInputInfo.dwTime;
            }         private void Form1_Load(object sender, EventArgs e)
            {
                this.timer1.Enabled = true;
                this.timer1.Interval = 2000;
            }
            private void timer1_Tick(object sender, EventArgs e)
            {
                long i = getIdleTick();
                this.Text = string.Format(" jinjazz说:您已经{0}ms没有动了 ", i);
                if (i > 2 * 1000)
                {
                    this.WindowState = FormWindowState.Minimized;
                }
                else
                {
                    this.WindowState = FormWindowState.Normal;
                } 
            } 
        }
    }
      

  4.   

    我在自己XP的机器上用vs2005建了一个工程,然后把上面这段代码复制黏贴,F5测试时只是打开一个窗口,并没有任何进一步的变化,而我把同一代码给朋友测试,他却可以窗口最小化,奇怪了