進程、多線程
阿新 • • 發佈:2017-10-09
lec als 關閉 microsoft 程序 sys tex 括號 tro
進程
進程類似於整個公司 - 一個進程 - 一個程序
Process.Start("");
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;//1引用命名空間 namespace WindowsFormsApplication5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //Process.Start("calc");//calc計算器括號內內要打卡的程序名字符串型的 //Process.Start("Chrome","http://www.baidu.com");//用谷歌的瀏覽器打開 //Process.Start("http://www.baidu.com");//用用戶電腦默認的瀏覽器打開 Process.Start(textBox1.Text);//4、打開 } private void button2_Click(object sender, EventArgs e) { //瀏覽並打開應用程序 //1、在from1中一個瀏覽按鈕一個打開按鈕一個TextBox用於接收瀏覽的路徑和一個OpenFileDialog openFileDialog1.Filter = "應用程序|*.exe";//3、只能看應用程序 DialogResult dr = openFileDialog1.ShowDialog(); if (dr == DialogResult.OK)//2、如果點的是確定將選中的文件在textBox1.Text顯示出來 { textBox1.Text = openFileDialog1.FileName; } } } }
線程
默認程序中只有一個線程 - 打個比方,線程就相當於整個公司的公司老板
同一時間只能做一件事 主線程 - 老板
線程 - 公司中的員工,臨時工
1、啟用線程
引用命名空間:using System.Threading;
Thread th = new Thread(Test1);
th.Start();
2、主線程/進程關閉後,子線程不會立刻退出
默認線程都是前臺線程
把前臺線程變為後臺線程
th.IsBackground = true;
3、默認是不允許跨線程訪問
關閉監控
Control.CheckForIllegalCrossThreadCalls = false;
4、只開啟一個線程
讓按鈕不可用
中間變量判斷
5、中止線程
th.Abort();
進程、多線程