C# 线程与进程

一、前台线程与后台线程对象

为什么要用多线程?

  1.让计算机“同时”做多件事情,节约时间。

  2.多线程可以让一个程序“同时”处理多个事情。

  3.后台运行程序,提高程序的运行效率,也不会导致主界面出现无响应的情况。

线程

- 前台线程:只有所有的前台线程都关闭才能完成程序关闭。

- 后台线程:只有所有的前台线程结束,后台线程自动结束。(设置方法:线程实例名.IsBackground = true)

1 using System.Threading;
线程命名空间

线程小示例:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 using System.Threading;
11 
12 namespace 线程
13 {
14     public partial class Form1 : Form
15     {
16         public Form1()
17         {
18             InitializeComponent();
19         }
20 
21         private void button1_Click(object sender, EventArgs e)
22         {
23             // 创建一个线程去执行这个方法
24             Thread th = new Thread(Test);
25             // 标记这个线程准备就绪,可以随时被执行.具体什么时候执行由CPU决定
26             // 前台线程和后台线程
27             // 将线程设置为后台线程
28             th.IsBackground = true;
29             th.Start();
30 
31         }
32         public void Test()
33         {
34             for (int i = 0; i < 2000; i++)
35             {
36                 Console.WriteLine(i);
37             }
38         }
39     }
40 }
View Code
 1 注:
 2     在.Net下,不允许跨线程访问
 3 
 4 
 5 可以在加载的时候,设置不检查线程:
 6 private void Form1_Load(object sender, EventArgs e)
 7         {
 8             // 取消跨线程的访问
 9             Control.CheckForIllegalCrossThreadCalls = false;
10         }

 注:处理线程抛异常情况(主线程已经释放,副线程还未结束,在窗体关闭时,判断线程的值)

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 using System.Threading;
11 
12 namespace 线程
13 {
14     public partial class Form1 : Form
15     {
16         public Form1()
17         {
18             InitializeComponent();
19         }
20         public Thread th;
21         private void button1_Click(object sender, EventArgs e)
22         {
23             // 创建一个线程去执行这个方法
24             th = new Thread(Test);
25             // 标记这个线程准备就绪,可以随时被执行.具体什么时候执行由CPU决定
26             // 前台线程和后台线程
27             // 将线程设置为后台线程
28             th.IsBackground = true;
29             th.Start();
30 
31         }
32         public void Test()
33         {
34             for (int i = 0; i < 20000; i++)
35             {
36                 //Console.WriteLine(i);
37                 textBox1.Text = i.ToString();
38             }
39         }
40 
41         private void Form1_Load(object sender, EventArgs e)
42         {
43             // 取消跨线程的访问
44             Control.CheckForIllegalCrossThreadCalls = false;
45         }
46 
47         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
48         {
49             // 当你点击关闭窗体的时候,判断新线程是否为null
50             if(th!=null)
51             {
52                 th.Abort(); // 关闭线程
53             }
54         }
55     }
56 }

 注:如果线程执行的方法需要参数,那么这个参数类型必须是object类型

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 using System.Threading;
11 
12 namespace 线程执行带参数的方法
13 {
14     public partial class Form1 : Form
15     {
16         public Form1()
17         {
18             InitializeComponent();
19         }
20 
21         private void button1_Click(object sender, EventArgs e)
22         {
23             Thread th = new Thread(Test);
24             th.IsBackground = true;
25             th.Start("123");
26         }
27         public void Test(object s)
28         {
29             for (int i = 0; i < 2000; i++)
30             {
31                 Console.WriteLine(i);
32             }
33         }
34     }
35 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 using System.Threading;
11 
12 namespace 线程之摇奖机
13 {
14     public partial class Form1 : Form
15     {
16         public Form1()
17         {
18             InitializeComponent();
19         }
20         Thread th;
21         bool b = false;
22         private void button1_Click(object sender, EventArgs e)
23         {
24             if (b == false)
25             {
26                 b = true;
27                 th = new Thread(PlayGame);
28                 th.IsBackground = true;
29                 th.Start();
30                 button1.Text = "停止";
31             }
32             else
33             {
34                 b = false;
35                 button1.Text = "开始";
36             }
37         }
38         public void PlayGame()
39         {
40             Random r = new Random();
41             while(b)
42             {
43                 Thread.Sleep(10);
44                 label1.Text = r.Next(0, 10).ToString();
45                 label2.Text = r.Next(0, 10).ToString();
46                 label3.Text = r.Next(0, 10).ToString();
47             }
48         }
49 
50         private void Form1_Load(object sender, EventArgs e)
51         {
52             Control.CheckForIllegalCrossThreadCalls = false;
53         }
54 
55         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
56         {
57             if(th!=null)
58             {
59                 th.Abort();
60             }
61         }
62     }
63 }
用线程执行摇奖机

进程

1 using System.Threading.Tasks;
进程命名空间
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Diagnostics;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace 进程
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             // 获取当前程序中所有正在运行的进程
15             //Process[] pros = Process.GetProcesses();
16             //foreach (var item in pros)
17             //{
18             //    // 杀死进程
19             //    // item.Kill();
20             //    Console.WriteLine(item);
21             //}
22 
23             // 通过进程打开一些应用程序
24             //Process.Start("calc");
25 
26             // 通过一个进程打开指定的文件
27             ProcessStartInfo psi = new ProcessStartInfo(@"C:\Users\sam\Desktop\控件命名规范.PNG");
28             Process p = new Process();
29             p.StartInfo = psi;
30             p.Start();
31             Console.ReadKey();
32         }
33     }
34 }
View Code
posted @ 2019-04-09 15:56  陈彦斌  阅读(753)  评论(0编辑  收藏  举报