您的位置:首页 > 编程语言 > C#

C#获取系统空闲时间

2012-03-19 22:23 176 查看
自己做的在用户不进行任何操作时,在设定时间后自动休眠,重启,关机。虽然不怎么实用,但其核心还是值得初学者学习的,就是用user32.dll获取系统空闲时间。下面是主要的实现代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace test
{
public partial class Form1 : Form
{
Process p = new Process();
string fun;
ulong time;

public struct lastInputInfo
{
public int cbsize;
public uint dwTime;
}

[DllImport("user32.dll")]
private static extern bool GetLastInputInfo(ref lastInputInfo plii);

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 1000;
}

private ulong GetIdleTick()
{
lastInputInfo LII = new lastInputInfo();
LII.cbsize = (int)Marshal.SizeOf(LII);
if (!GetLastInputInfo(ref LII))
{
return 0;
}
return (ulong)Environment.TickCount - LII.dwTime;
}

private void timer1_Tick(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = (GetIdleTick() / 1000).ToString();

if (GetIdleTick() / 1000 == time * 60)
{
switch (fun)
{
case "定时休眠":
timer1.Stop();
hibernation();
break;
case "定时重启":
timer1.Stop();
restart();
break;
case "定时关机":
timer1.Stop();
shutdown();
break;
default:
break;
}
}
}

private void btn_Hibernation_Click(object sender, EventArgs e)
{
if (txt_time.Text == "")
{
MessageBox.Show("请输入一个整数以设定时间","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
fun = "定时休眠";
time = Convert.ToUInt64(txt_time.Text.Trim());
txt_time.ReadOnly = true;
timer1.Start();
}

private void btn_Restart_Click(object sender, EventArgs e)
{
if (txt_time.Text == "")
{
MessageBox.Show("请输入一个整数以设定时间", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
fun = "定时重启";
time = Convert.ToUInt64(txt_time.Text.Trim());
txt_time.ReadOnly = true;
timer1.Start();
}

private void btn_Shutdown_Click(object sender, EventArgs e)
{
if (txt_time.Text == "")
{
MessageBox.Show("请输入一个整数以设定时间", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
fun = "定时关机";
time = Convert.ToUInt64(txt_time.Text.Trim());
txt_time.ReadOnly = true;
timer1.Start();
}

public void shutdown()
{
p.StartInfo.FileName = "shutdown.exe";
p.StartInfo.Arguments = "/s";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
}

public void restart()
{
p.StartInfo.FileName = "shutdown.exe";
p.StartInfo.Arguments = "/r";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
}

public void hibernation()
{
p.StartInfo.FileName = "shutdown.exe";
p.StartInfo.Arguments = "/h";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
}

private void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Visible = false;
NotifyIcon1.Visible = true;
}
}

private void exit_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
this.Visible = true;
NotifyIcon1.Visible = false;
WindowState = FormWindowState.Normal;
}

private void btn_reset_Click(object sender, EventArgs e)
{
timer1.Stop();
toolStripStatusLabel1.Text = (GetIdleTick() / 1000).ToString();
txt_time.ReadOnly = false;
txt_time.Text = "";
}
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# timer object fun user struct