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

为什么要用多线程?

  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# 线程与进程的更多相关文章

  1. Python 【第五章】:线程、进程和协程

    Python线程 Threading用于提供线程相关的操作,线程是应用程序中工作的最小单元. #!/usr/bin/env python # -*- coding:utf-8 -*- import t ...

  2. Python网络编程之线程,进程

    一. 线程: 基本使用 线程锁 线程池 队列(生产者消费者模型) 二. 进程:  基本使用  进程锁 进程池 进程数据共享 三. 协程: gevent greenlet 四. 缓存: memcache ...

  3. iOS开发:(线程篇-上)线程和进程

    iOS开发多线程篇—多线程简单介绍 一.进程和线程 1.什么是进程 进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 比如同时打开QQ.Xcod ...

  4. LR中线程和进程的区别

    LoadRunner中的进程与线程    1.进程与线程的区别: 进程和线程的区别是什么?进程和线程都是由操作系统所体会的程序运行的基本单元,系统利用该基本单元实现系统对应用的并发性.进程和线程的区别 ...

  5. iOS_线程和进程的区别与联系

    首先是线程和进程的联系: 线程和进程都是由操作系统所负责的程序运行的基本单元,系统利用该基本单元实现对应用的并发性. 接下来就是线程和进程的区别: 线程和进程最大的区别就是它们是操作系统的两种资源管理 ...

  6. python学习笔记12 ----线程、进程

    进程和线程的概念 进程和线程是操作系统中两个很重要的概念,对于一般的程序,可能有若干个进程,每一个进程有若干个同时执行的线程.进程是资源管理的最小单位,线程是程序执行的最小单位(线程可共享同一进程里的 ...

  7. python任务执行之线程,进程,与协程

    一.线程 线程为程序中执行任务的最小单元,由Threading模块提供了相关操作,线程适合于IO操作密集的情况下使用 #!/usr/bin/env python # -*- coding:utf-8 ...

  8. 线程 VS 进程

    线程是指进程内的一个执行单元,也是进程内的可调度实体. 与进程的区别: (1)地址空间:进程内的一个执行单元;进程至少有一个线程;它们共享进程的地址空间;而进程有自己独立的地址空间; (2)资源拥有: ...

  9. Python之线程、进程和协程

    python之线程.进程和协程 目录: 引言 一.线程 1.1 普通的多线程 1.2 自定义线程类 1.3 线程锁 1.3.1 未使用锁 1.3.2 普通锁Lock和RLock 1.3.3 信号量(S ...

  10. Python 线程、进程和协程

    python提供了两个模块来实现多线程thread 和threading ,thread 有一些缺点,在threading 得到了弥补,为了不浪费时间,所以我们直接学习threading 就可以了. ...

随机推荐

  1. jq 工具及其常用用法

    在处理 JSON 数据时,我们经常需要在命令行中进行过滤.查询和编辑的操作.jq 是一个强大的命令行 JSON 处理工具,它可以让我们轻松地对 JSON 数据进行各种操作.本文将简要介绍 jq 的基本 ...

  2. kubernets之了解Qos等级

    一  Qos的种类 BestEffort(优先级最低) Burstable(中等优先级) Guaranteed(最高优先级) 二  Qos的作用 众所周知,节点上面的limits允许超卖,当节点上面的 ...

  3. Gin 框架的执行流程

    Gin框架是一个用Go语言编写的高性能Web框架,它基于httprouter实现,具有快速.简洁和高效的特性. 以下是Gin框架处理HTTP请求的大致执行流程: 1 初始化Gin引擎: 用户创建一个新 ...

  4. android studio 安装与配置

    android  studio  下载地址:http://www.android-studio.org/ 找一个存储空间,我在D盘上,建好如下目录 : 找到刚才在载的文件    android-stu ...

  5. exe应用程序安装为windows服务

    1.使用instsrv.exe和srvany.exe 当你获取到srvany后并决定将某程序作为服务启动后,请先将srvany安装为系统服务,具体的安装方法有很多,这里使用instsrv,语法如下:安 ...

  6. k8s——pod(label和selector)

    k8s的label和selector 在Kubernetes中,label和selector是两个重要的概念,它们一起用于实现资源对象的关联和调度. label 创建label 有两种方式创建labe ...

  7. react 高阶函数

    HOC(Higher Order Components)就是一个函数,传给它一个组件,它返回一个新的组件. 高阶组件:就相当于手机壳,通过包装组件,增强组件功能. 实现步骤: 首先创建一个函数 指定函 ...

  8. P1737

    problem \(\text{task 1}\) 要求: 输入:\(a,b\). 输出:\(-2a-2b\). 数据范围:\(|a|,|b| \le 10^9\). 做法: 先把 \(-2\) 提出 ...

  9. ABC342

    E 建反图 + 拓扑排序. 先求出直接与 \(n\) 连接的点的答,就是最后一辆车的发车时间.然后再做拓扑排序. 假如我们知道点 \(u\) 的答案为 \(ans_u\) 并且 \(u,v\) 相连, ...

  10. netcore 打包dll发布到nuget服务器

    可参考微软官网:NuGet.org 概述 | Microsoft Docs 一.创建类库 首先创建一个类库,就是你想要发布到nuget的类库,生成项目 二.下载并注册nuget nuget地址:htt ...