如何判断鼠标键盘长时间没有操作和CPU长时间低于某值呢? 如题。我的目的是自己做一个关机软件。判断关机的条件是:1。CPU占用率长期稳定于一个低水平,内存也一样2。鼠标键盘长时间没有操作市面上有一款【自动关机3000】的,只能用在一台机器上,所以想自己搞搞,咋入手呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1. 查看CPU使用率,msdn上有下载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] 代码如下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; } } }} 我在自己XP的机器上用vs2005建了一个工程,然后把上面这段代码复制黏贴,F5测试时只是打开一个窗口,并没有任何进一步的变化,而我把同一代码给朋友测试,他却可以窗口最小化,奇怪了 timer的问题,请大师给我提点提点,我是初学者 winform里窗口的滚动条问题 请教:用C#.net如何实现数据库中一张表的数据复制到另外一张表? c#获取主板序列号 奇怪的麻烦 模糊查询的问题 新手求解!!!!!!!!!!!!!!!!! 存储过程◎!◎◎ 请教如何做一个类似 google bar 的工具条啊?在IE上面?msdn 没看懂,用c#做 .net有没有MSFlexgrid这样类似的网格控件没有? 1000分求购C#读取TDrawGrid表格数据源代码 combobox 如何列表检索?
2. 检查空闲时间
[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]
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;
}
}
}
}