由於聽說某server開着就很貴,因此咱們跑完測試的job後就要趕忙關機才行,可是測試的job要跑好久,過程當中又不須要幹什麼,因此就得有個守家的,有時候會走很晚。若是有一個自動化關機的工具就行了,當指定的進程結束了之後系統就會自動關機。這件事我在上一篇中已經作好了。這一次領導又有新需求,說要監控多個進程而不僅僅是一個了,須要有一個配置文件來配置所須要監控的進程名,並且想要能夠自主選擇檢查的間隔,因而就有了下文。ide
代碼以下:工具
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; using System.Text.RegularExpressions; using System.IO; namespace AutoShutDown2 { public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private void chooseFileButton_Click(object sender, EventArgs e) { OpenFileDialog fileName = new OpenFileDialog(); fileName.Filter = "文本文件|*.*|C#文件|*.cs|全部文件|*.*"; if (fileName.ShowDialog() == DialogResult.OK) { filePath.Text = fileName.FileName; } } private void filePath_Click(object sender, EventArgs e) { filePath.Text = ""; } private void startButton_Click(object sender, EventArgs e) { if (filePath.Text.ToString().Substring(filePath.Text.Length - 3, 3) == "txt") { if (Regex.IsMatch(duration.Text, "^([0-9]{1,})$")) { if (int.Parse(duration.Text) >= 10) { MessageBox.Show("PCAS will check with a duration of " + duration.Text + "s."); this.Hide(); //Check the processes with the duration. DurationStart(); } else { MessageBox.Show("The integer number should be greater than 10 seconds."); } } else { MessageBox.Show("You can only type in an integer for duration."); duration.Text = ""; } } else { MessageBox.Show("You can only choose a txt to be a configuration file."); filePath.Text = ""; } } private void DurationStart() { //Check the process's status with the duration. System.Timers.Timer tmr = new System.Timers.Timer(int.Parse(duration.Text)*1000); tmr.Elapsed += new System.Timers.ElapsedEventHandler(CheckProcess); tmr.AutoReset = true; tmr.Enabled = true; } private void CheckProcess(object source, System.Timers.ElapsedEventArgs e) { //Check the processes's status in the config file. FileStream fs = new FileStream(filePath.Text, FileMode.Open); StreamReader sr = new StreamReader(fs); string line; int numOfTheProcesses = 0; while ((line = sr.ReadLine()) != null) { var processes = System.Diagnostics.Process.GetProcesses(); foreach (var process in processes) { if (process.ProcessName == line) { //Find the objective process. //MessageBox.Show(line); numOfTheProcesses++; } } } if (numOfTheProcesses == 0) { //No such process, shut down the computer. //MessageBox.Show("The computer is ready to be shut down."); //Shut down the computer ShutDown(); } sr.Close(); fs.Close(); } private void ShutDown() { //Shut down the computer. 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"); } } }
你須要輸入一個大於10的整數,而且填寫的路徑必定要是一個txt文本。不然會給予提示。測試
配置文件中的配置是這樣的,每一行填寫一個要監控的進程名:this
路徑和時間間隔填寫正確之後點擊Start就會開始按你設定的時間間隔自動監控配置文件中的全部進程,等到全部進程都中止後系統就會自動關機。spa