2017年11月28日 C#进程和线程

进程

需要放using System.Diagnostics;才可以用进程

用时的方法名为Process

用两个按钮一个为选择文件夹一个为打开可以打开系统内的进程.

注意:打开时一定要用进程名

Process.Start("进程名");

Process p = new Process();

ProcessStartInfo psi = new ProcessStartInfo(要开启的进程的绝对路径);

p.StartInfo = psi;

p.Start();

用默认的浏览器打开百度网址

Process.Strart("http://www.baidu.com");

用一个按钮控制关闭所有进程

Process[] ps = Process.GetProcesses();
foreach (Process p in ps)
{
    p.Kill();
}

线程

需先引入using System.Threading;才可以使用

Thread.Sleep(这里面放的是毫秒) //做延时用

前台线程与后台线程

Thread th = new Thread(委托); //创建对象
th.IsBackground = true; //设置后台线程,删掉这一句就可以变为前台线程
th.Start(); //开始执行

允许跨窗体访问控件.要在构造函数里加入这么一句

Control.CheckForIllegalCrossThreadCalls = false;

就可以跨窗体访问控件了

原文地址:https://www.cnblogs.com/zJuevers/p/7911592.html