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 TestOfProcess
{
public partial class Form1 : Form
{
Thread thread1;
int num = 0;
bool isRun=true;
public Form1()
{
InitializeComponent();
thread1 = new Thread(new ThreadStart(thread1CallBack));
}
public void thread1CallBack()
{
while(true)
while (isRun)
{
num++;
Thread.Sleep(1000);
}
}
private void button1_Click(object sender, EventArgs e)
{
Process process1 = new Process();
process1.StartInfo.FileName = "notepad.exe";
//启动Notepad.exe进程.
process1.Start();
}
private void button2_Click(object sender, EventArgs e)
{
Process[] process;
process = Process.GetProcessesByName("notepad");
foreach(Process instance in process)
{
instance.WaitForExit(1000);
instance.CloseMainWindow();
}
}
private void button3_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));
}
}
}
private void button4_Click(object sender, EventArgs e)
{
thread1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
richTextBox1.AppendText(num.ToString());
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
isRun = (sender as CheckBox).Checked;
}
}
}
转载于:https://www.cnblogs.com/because/archive/2012/03/24/2416012.html