今天QA部门需要进行Performance测试,因为跑job的时间会很长,下班也跑不完。所以想要做一个job运行完毕自动关机的工具。

原理就是检查进程的名称,如果检查不到相应的进程,就说明job已经跑完了,可以关机了。

下图是我做的自动关机工具,选择相应的进程名(这里选择job的进程名),点击OK之后窗体会隐藏,在后台监控进程状态:

程序实例只能运行一个,禁止多个实例同时运行,如果后台已经存在实例,再点击打开工具会提示:

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics; namespace AutoShutDown
{
public partial class MainForm : Form
{
public MainForm()
{
//MessageBox.Show(Process.GetCurrentProcess().ProcessName.ToString());
//Only one instance can run at the same time.
Process[] tylan = Process.GetProcessesByName("AutoShutDown");
if (tylan.Count() > )
{
MessageBox.Show("Sorry, the instance of this program has already run on this computer. You can not run it again.");
System.Diagnostics.Process.GetCurrentProcess().Kill();
}
InitializeComponent();
AddProcesses();
} private void AddProcesses()
{
var processes = System.Diagnostics.Process.GetProcesses();
foreach (var process in processes)
{
Processes.Items.Add(process.ProcessName.ToString());
}
} private void Processes_SelectedIndexChanged(object sender, EventArgs e)
{
Processes.Text = Processes.SelectedItem.ToString();
} private void OKButton_Click(object sender, EventArgs e)
{
//Check if the process has been selected.
if (Processes.Text == "")
{
MessageBox.Show("Please select a process first.");
}
else
{
//Check the process's status every 5 miniutes.
System.Timers.Timer tmr = new System.Timers.Timer(5000);
tmr.Elapsed += new System.Timers.ElapsedEventHandler(CheckProcess);
tmr.AutoReset = true;
tmr.Enabled = true;
this.Hide();
}
} private void CheckProcess(object source, System.Timers.ElapsedEventArgs e)
{
int numOfTheProcesses = ;
var processes = System.Diagnostics.Process.GetProcesses();
foreach (var process in processes)
{
string TheProcessName = "";
if (Processes.InvokeRequired)
{
Processes.Invoke(new MethodInvoker(delegate { TheProcessName = Processes.Text; }));
}
if (process.ProcessName == TheProcessName)
{
//Find the objective process.
//MessageBox.Show(TheProcessName);
numOfTheProcesses++;
}
}
if (numOfTheProcesses == )
{
//No such process, shut down the computer.
MessageBox.Show("The computer is ready to be shut down.");
//Shut down the comp
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
myProcess.StandardInput.WriteLine("shutdown -s -t 0");
}
}
}
}

其中橘黄色字体部分为关机代码,红色字体部分为每五秒钟执行一次ChecProcess方法,每五秒检查一次进程是否存在,如果不存在了,就关机。

最新文章

  1. Apache多站点配置及启动失败解决办法
  2. SQL语句的用法
  3. 如何重置mysql的密码
  4. PERCONA-TOOLKIT 工具的安装与使用2
  5. 【转】silverlight 跨域访问
  6. 新手们的GDI+绘制方格
  7. maven, sesame, openrdf, eclipse 的初始学习
  8. URL vs. HTML 录制模式
  9. Samba(一)通过Samba搭建Linux文件服务器
  10. linkButton
  11. (function(root,factory){})(this,function($){}) 一个立即执行的匿名函数自调
  12. 晒stlink以及stm8“开发板”
  13. admin 配置
  14. Python的数据库操作(pymysql)
  15. 19-02【mac电脑操作】最小化应用程序
  16. Confluence 6 Windows 中以服务方式自动重启的原因
  17. 监控端口是否开放,端口未开放关闭虚拟ip,端口开放启动虚拟IP
  18. 值不能为 null。 参数名: source
  19. win7下面iis错误汇总
  20. Bootstrap Table使用方法详解

热门文章

  1. ajax-原理分析
  2. 读源码 | metisMenu侧边栏插件
  3. webservice系统学习笔记9-使用契约优先的方式的一个服务端demo(隐式传Header信息)
  4. 类的专有方法(__init__)
  5. ContextMenu上下文菜单
  6. java中的 public protected friendly private
  7. 百度定位SDK实现获取当前经纬度及位置
  8. android程序监听home键与电源键
  9. Android 手机震动功能实现
  10. Concurrency Managed Workqueue(三)创建workqueue代码分析