C#编程应用--进程管理

来源:互联网 发布:什么软件可以淘宝返利 编辑:程序博客网 时间:2024/05/19 16:19

 http://www.cnblogs.com/dzone/archive/2011/09/28/2194909.html

 
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Diagnostics;
using System.Threading;  namespace ProcessExample {      public partial class Form1 : Form     {          public Form1()         {             InitializeComponent();         }          private void buttonStart_Click(object sender, EventArgs e)         {             process1.StartInfo.FileName = "notepad.exe";             //启动Notepad.exe进程.             process1.Start();         }         private void buttonStop_Click(object sender, EventArgs e)         {             //创建新的Process组件的数组,并将它们与指定的进程名称(Notepad)的所有进程资源相关联.             Process[] myprocesses;             myprocesses = Process.GetProcessesByName("Notepad");             foreach (Process instance in myprocesses)             {                 //设置终止当前线程前等待1000毫秒                 instance.WaitForExit(1000);                 instance.CloseMainWindow();             }         }         private void buttonView_Click(object sender, EventArgs e)         {             listBox1.Items.Clear();             //创建Process类型的数组,并将它们与系统内所有进程相关联             Process[] processes;             processes = Process.GetProcesses();             foreach (Process p in processes)             {                 //Idle指显示CPU空闲率的进程名称                 //由于访问Idle的StartTime会出现异常,所以将其排除在外                 if (p.ProcessName != "Idle")                 {                     //将每个进程名和进程开始时间加入listBox1中                     this.listBox1.Items.Add(                     string.Format("{0,-30}{1:h:m:s}", p.ProcessName, p.StartTime));                 }             }         }     } }