C#获取系统空闲时间
自己做的在用户不进行任何操作时,在设定时间后自动休眠,重启,关机。虽然不怎么实用,但其核心还是值得初学者学习的,就是用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#获取系统空闲时间相关推荐
- VC获取系统空闲时间
LASTINPUTINFO lpi; lpi.cbSize = sizeof(lpi); GetLastInputInfo(&lpi); int iTime ...
- c 获取当前时间插入oracle数据库,Oracle如何获取系统当前时间等操作实例,c++获取系统时间...
Oracle如何获取系统当前时间等操作实例,c++获取系统时间 获取系统当前时间 date类型的 select sysdate from dual; char类型的 select to_char(sy ...
- oracle 取系统当前年份_Oracle如何获取系统当前时间等操作实例
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 获取系统当前时间 date类型的 ? 1 select sysdate from dual; char类型的 ? 123456 select to_cha ...
- C/C++获取系统当前时间
C/C++获取系统当前时间 C库中与系统时间相关的函数定义在<time.h>头文件中, C++定义在<ctime>头文件中. 一.time(time_t*)函数 函数定义如下: ...
- sap获取系统时间_获取系统当前时间
NSString *appKey() { // // 获取系统当前时间 // NSDate * date = [NSDate date]; // NSTimeInterval sec = [date ...
- java获取系统当前时间格式化_java 获取系统当前时间并格式化
java 获取系统当前时间并格式化 CreateTime--2018年5月9日11:41:00 Author:Marydon 实现方式有三种 updateTime--2018年7月23日09点32分 ...
- java获取空闲端口_获取系统空闲端口
端口取值范围 以下搜自互联网 一般用到的是1到65535,其中0不使用,1-1023为系统端口,也叫BSD保留端口;1024-65535为用户端口,又分为: BSD临时端口(1024-5000)和BS ...
- android service 样例(电话录音和获取系统当前时间)
关于android service 的具体解释请參考: android四大组件--android service具体解释.以下将用两个实例具体呈现Android Service的两种实现. 一个是st ...
- 获取系统当前日期时间
今天学习了一个获取系统当前日期时间的方式. 代码如下: #include <iostream> #include <ctime> using namespace std; in ...
最新文章
- 算法必知 --- LRU缓存淘汰算法
- 02-线性结构3 Reversing Linked List
- [BSidesSF2020]decrypto-2
- filebeat+elk简单搭配
- [Qt教程] 第31篇 网络(一)Qt网络编程简介
- java 各种数据类型的互相转换
- 中国基于散射的光学传感器行业市场供需与战略研究报告
- 005. C#发送邮件
- linux 反汇编运行时代码段,linux内核学习之一 简单c语言反汇编(示例代码)
- 肯德尔相关性分析_肯德尔的Tau机器学习相关性
- device-side assert triggered原因和解决方法
- Delta RPMs disabled because /usr/bin/applydeltarpm not installed.问题解决记录
- Python应用(四)其他功能(一)python将ppt导出高清图片
- [渝粤题库]西北工业大学电工与电子技术
- 从利用gensim 训练好的word2vec模型中取出向量并计算余弦相似度,Levenshtein distance 计算
- Java Swing 设置全局的字体
- 一文读懂工业设计中心的申报条件-深科信
- Scrapy简明教程(一)
- 大点干!早点散----------深入剖析GFS分布式文件系统
- 极客日报:曝OPPO给离职员工补发年终奖,此前遭克扣;7 亿条领英用户数据被出售;Android Studio 4.2.2 发布