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 }
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 }
C# 线程与进程的更多相关文章
- Python 【第五章】:线程、进程和协程
Python线程 Threading用于提供线程相关的操作,线程是应用程序中工作的最小单元. #!/usr/bin/env python # -*- coding:utf-8 -*- import t ...
- Python网络编程之线程,进程
一. 线程: 基本使用 线程锁 线程池 队列(生产者消费者模型) 二. 进程: 基本使用 进程锁 进程池 进程数据共享 三. 协程: gevent greenlet 四. 缓存: memcache ...
- iOS开发:(线程篇-上)线程和进程
iOS开发多线程篇—多线程简单介绍 一.进程和线程 1.什么是进程 进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 比如同时打开QQ.Xcod ...
- LR中线程和进程的区别
LoadRunner中的进程与线程 1.进程与线程的区别: 进程和线程的区别是什么?进程和线程都是由操作系统所体会的程序运行的基本单元,系统利用该基本单元实现系统对应用的并发性.进程和线程的区别 ...
- iOS_线程和进程的区别与联系
首先是线程和进程的联系: 线程和进程都是由操作系统所负责的程序运行的基本单元,系统利用该基本单元实现对应用的并发性. 接下来就是线程和进程的区别: 线程和进程最大的区别就是它们是操作系统的两种资源管理 ...
- python学习笔记12 ----线程、进程
进程和线程的概念 进程和线程是操作系统中两个很重要的概念,对于一般的程序,可能有若干个进程,每一个进程有若干个同时执行的线程.进程是资源管理的最小单位,线程是程序执行的最小单位(线程可共享同一进程里的 ...
- python任务执行之线程,进程,与协程
一.线程 线程为程序中执行任务的最小单元,由Threading模块提供了相关操作,线程适合于IO操作密集的情况下使用 #!/usr/bin/env python # -*- coding:utf-8 ...
- 线程 VS 进程
线程是指进程内的一个执行单元,也是进程内的可调度实体. 与进程的区别: (1)地址空间:进程内的一个执行单元;进程至少有一个线程;它们共享进程的地址空间;而进程有自己独立的地址空间; (2)资源拥有: ...
- Python之线程、进程和协程
python之线程.进程和协程 目录: 引言 一.线程 1.1 普通的多线程 1.2 自定义线程类 1.3 线程锁 1.3.1 未使用锁 1.3.2 普通锁Lock和RLock 1.3.3 信号量(S ...
- Python 线程、进程和协程
python提供了两个模块来实现多线程thread 和threading ,thread 有一些缺点,在threading 得到了弥补,为了不浪费时间,所以我们直接学习threading 就可以了. ...
随机推荐
- 怎么样给Oracle数据库中的表添加列?
首发微信公众号:SQL数据库运维 原文链接:https://mp.weixin.qq.com/s?__biz=MzI1NTQyNzg3MQ==&mid=2247485212&idx=1 ...
- js原生小知识
new Array(3).fill(0) 就会生成3个元素为0的数组
- IDEA的Ctrl+Enter补全代码失效
前景提示 IDEA有个ctrl+enter可以补全代码的功能,但是,今天突然失效了,原来是这个问题. 修改配置 进入setting修改 进入Edit-->找到Intertions,搜素Intro ...
- Dapr 与 .NET Aspire 结合使用获得无与伦比的本地开发体验
Dapr 提供了一组构建块,用于抽象分布式系统中常用的概念.这包括服务.缓存.工作流.复原能力.机密管理等之间的安全同步和异步通信.不必自己实现这些功能,可以消除样板,降低复杂性,并允许您专注于开发业 ...
- MySQL面试必备三之事务
本文首发于公众号:Hunter后端 原文链接:MySQL面试必备三之事务 这一篇笔记介绍一下 MySQL 的事务,面试中常被问到关于事务的几个问题如下: 事务是什么 为什么需要事务,事务有什么作用 事 ...
- Pandas学习之路【3】
新增列的一些操作 1.新增一个列,直接给列赋值 # 取所有行,新增的列为new_col df.loc[:, 'new_col'] = 100 2.使用df.apply方法给新增的列赋值 def get ...
- WPF开发快速入门【1】WPF的布局
概述 本文描述几款WPF中常用的布局控件. Grid Grid是WPF最常用的布局控件. 它把面板分割为固定长和宽的网格,子控件就放置在网格内. <Grid> <Grid.Colum ...
- minio-搭建个人云存储服务
相信风靡全球的亚马逊 AWS S3 的存储云服务大家已经耳熟能详了,如何自己搭建一个私有的S3存储云服务呢?Minio 提供对象存储服务,兼容了 AWS S3 存储协议,用于非结构化的数据存.非结构化 ...
- springboot~封装依赖引用包jar还是pom,哪种更规范
将多个第三方包封装成一个项目后,如果你的目的是让其他开发人员可以直接引用这些依赖,一般来说有两种常见的方式: 打成JAR包:将封装好的项目编译打包成JAR文件,其他开发人员可以将这个JAR文件添加到他 ...
- openssl升级nginx升级支持openssl http2
mkdir -p /usr/local/openssl #wget https://www.openssl.org/source/openssl-1.1.1d.tar.gz tar -xf opens ...