• 用C#写一个实现进程监控的自动关机工具


    今天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() > 1)
                {
                    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 = 0;
                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 == 0) 
                {
                    //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方法,每五秒检查一次进程是否存在,如果不存在了,就关机。

  • 相关阅读:
    表单校验神器
    插入排序
    数组去重的几种常使用的方式
    day44 mysql高级部分内容
    day43 多表查询和pymysql
    day42 字段的增删改查详细操作
    day41 mysql详细操作
    day40 mysql数据类型
    day39 mysql数据库基本操作
    day37 异步回调和协程
  • 原文地址:https://www.cnblogs.com/LanTianYou/p/4673233.html
Copyright © 2020-2023  润新知