源码如下:
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.Diagnostics;
using System.Threading;
namespace ProcessManageApp
{
public partial class SYSProcessManageForm : Form
{
public SYSProcessManageForm()
{
InitializeComponent();
}
private void startNoteButton_Click(object sender, EventArgs e)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "notepad.exe";
//参数
//process.StartInfo.Arguments = "http://www.hacktea8.com";
process.Start();
//系统 重启、关机
/*
System.Diagnostics.Process.Start("rundll32.exe","user.exe,restartwindows");
System.Diagnostics.Process.Start("rundll32.exe","user.exe,exitwindows");
*/
}
private void stopNoteButton_Click(object sender, EventArgs e)
{
//创建process数组与notepad进程资源相关联
Process[] myprocess = Process.GetProcessesByName("Notepad");
if(myprocess.Length == 0){
return;
}
foreach(Process instance in myprocess){
//终止线程当前等待的1000毫秒
instance.WaitForExit(1000);
instance.CloseMainWindow();
}
}
private void watchButton_Click(object sender, EventArgs e)
{
//清理listBox条目
resultListBox.Items.Clear();
Process[] process = Process.GetProcesses();
foreach(Process p in process){
resultListBox.Items.Add(p.ProcessName);
}
}
}
}
效果图如下: