服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#多线程之Thread类详解

C#多线程之Thread类详解

2022-01-19 11:36云梦鸿 C#

这篇文章主要为大家详细介绍了C#多线程之Thread类,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

使用system.threading.thread类可以创建和控制线程。

常用的构造函数有:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 摘要:
 //  初始化 system.threading.thread 类的新实例,指定允许对象在线程启动时传递给线程的委托。
 //
 // 参数:
 // start:
 //  system.threading.parameterizedthreadstart 委托,它表示此线程开始执行时要调用的方法。
 //
 // 异常:
 // system.argumentnullexception:
 //  start 为 null。
 [securitysafecritical]
 public thread(parameterizedthreadstart start);
 //
 // 摘要:
 //  初始化 system.threading.thread 类的新实例。
 //
 // 参数:
 // start:
 //  system.threading.threadstart 委托,它表示此线程开始执行时要调用的方法。
 //
 // 异常:
 // system.argumentnullexception:
 //  start 参数为 null。
 [securitysafecritical]
 public thread(threadstart start);

1. 无参数创建线程

threadstart委托定义了一个返回类型位void的无参数方法。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void main()
  {
   thread vthread = new thread(threadfun);
   //vthread.name = "td_name"; // 线程名称
   vthread.start(); //开始执行线程
   console.writeline("this is the main thread:id=" + thread.currentthread.managedthreadid);
  }
 
  void threadfun() // 来自委托:threadstart
  {
   console.writeline("running in a new thread:id=" + thread.currentthread.managedthreadid);
   for (int i = 0; i < 10; i++)
   {
    console.write(".");
    thread.sleep(500);
   }
   console.writeline("thread end");
  }

输出结果:

C#多线程之Thread类详解

将上诉代码中的 threadfun() 用lambda表达式替换,变成thread的简便使用方式:

?
1
2
3
4
5
6
7
8
9
10
public void main()
 {
  thread vthread = new thread(() =>
  {
   console.writeline("running in a new thread");
  });
  //vthread.name = "td_name"; // 线程名称
  vthread.start(); //开始执行线程
  console.writeline("this is the main thread");
 }

2.给线程传递参数

两种方式:一种是使用带parameterizedthreadstart委托的方法参数构造thread;另一种是创建一个自定义类,把线程的方法定义为实例方法,这样先初始化实例的数据,在启动线程。

如:传递参数

?
1
2
3
4
public struct tddata // 传递数据
  {
   public string message; //数据string字段
  }

使用第一种方式:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
public void main()
 {
  tddata tdata = new tddata() { message = "thread info" };
  thread vthread = new thread(threadfun);
  vthread.start(tdata); // 开始执行线程,传递参数
  console.writeline("this is the main thread");
 }
 
 void threadfun(object pobj) // 来自委托:parameterizedthreadstart
 {
  tddata vdata = (tddata)pobj;
  console.writeline("in a new thread, received:{0}", vdata.message);  
 }

使用第二种方式:先自定义一个类。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class tdhelper
 {
  public tddata mdata; // 传递数据
 
  // 构造函数
  public tdhelper(tddata pdata)
  {
   this.mdata = pdata;
  }
 
  public void threadfun() // 来自委托:threadstart
  {
   console.writeline("in a new thread, tddatamessage:{0}", mdata.message);
  }
 }

然后,在主线程(需要的地方)创建thread并将实例方法tdhelper.threadfun()作为构造函数的参数。

?
1
2
3
4
5
6
7
8
public void main()
  {
   tddata tdata = new tddata() { message = "thread info" };
   tdhelper thelper = new tdhelper(tdata); // 传递参数
   thread vthread = new thread(thelper.threadfun);
   vthread.start();
   console.writeline("this is the main thread");
  }

3.后台线程

默认情况下,thread类创建的线程事前台线程,线程池中的线程总是后台线程。只要有一个前台线程在运行,应用程序的进程就在运行,如果多个前台线程在运行,而main()方法结束了,应用程序仍然事激活的,直到所有前台线程完成任务。

可以通过设置thread类实例的isbackground属性,来让其成为后台线程;

?
1
2
3
4
5
6
7
8
9
10
11
12
public void main()
  {
   thread vthread = new thread(() =>
   {
    console.writeline("new thread started"); // title3
    thread.sleep(5000);
    console.writeline("new thread completed"); // title2
   });
   //vthread.isbackground = true;
   vthread.start();
   console.writeline("this is the main thread"); // title1
  }

 当isbackground属性默认为false时,可以在控制台完整地看到 3 句输出信息;但如果将其设为true时,则不等到第3条信息(title2)输出时,主线程main()已经执行完成,控制台窗口就自动关闭了。

4.线程的优先级

通过priority属性,可以调整thread类实例的优先级,默认为: vthread.priority = threadpriority.normal; // 枚举值

关系:highest > abovenormal > normal > belownormal > lowest

5.控制线程

调用thread对象的start()方法,可以创建线程。但是,在调用start()方法后,新线程仍不是处于 running 状态,而是 unstarted 状态。只有操作系统的线程调度器选择了要运行该线程,线程就会改为 running 状态。通过 thread.threadstate 属性,可以获得该线程当前的状态。

使用thread.sleep()方法,会使线程处于waitsleepjoin状态,在经历sleep()方法定义的时间段后,线程就会等待再次被操作系统调度。

要停止一个线程,可以调用 thread.abort() 方法。调用这个方法,会在接到终止命令的线程中抛出一个 threadabortexception,用一个处理程序捕获这个异常,线程可以在结束前完成一些清理工作。线程还可以在接收到调用 thread.abort() 方法的结果 threadabortexception 异常后继续工作。如果线程没有重置终止,接收到终止请求的线程的状态就从 abortrequested 改为 aborted 。

如果要等待线程结束,就可以调用 thread.join() 方法,它会停止当前线程,并把它设置为 waitsleepjoin 状态,直到加入的线程完成为止。

参见: c#多线程之线程控制详解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/CUIT-DX037/p/6952092.html

延伸 · 阅读

精彩推荐
  • C#Unity3D UGUI实现缩放循环拖动卡牌展示效果

    Unity3D UGUI实现缩放循环拖动卡牌展示效果

    这篇文章主要为大家详细介绍了Unity3D UGUI实现缩放循环拖动展示卡牌效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参...

    诗远3662022-03-11
  • C#C# 后台处理图片的几种方法

    C# 后台处理图片的几种方法

    本篇文章主要介绍了C# 后台处理图片的几种方法,非常具有实用价值,需要的朋友可以参考下。...

    IT小伙儿10162021-12-08
  • C#c#学习之30分钟学会XAML

    c#学习之30分钟学会XAML

    一个界面程序的核心,无疑就是界面和后台代码,而xaml就是微软为构建应用程序界面而创建的一种描述性语言,也就是说,这东西是搞界面的...

    C#教程网8812021-12-10
  • C#C#基础之泛型

    C#基础之泛型

    泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能。接下来通过本文给大家介绍c#基础之泛型,感兴趣的朋友一起学习吧...

    方小白7732021-12-03
  • C#C#直线的最小二乘法线性回归运算实例

    C#直线的最小二乘法线性回归运算实例

    这篇文章主要介绍了C#直线的最小二乘法线性回归运算方法,实例分析了给定一组点,用最小二乘法进行线性回归运算的实现技巧,具有一定参考借鉴价值,需要...

    北风其凉8912021-10-18
  • C#浅谈C# winForm 窗体闪烁的问题

    浅谈C# winForm 窗体闪烁的问题

    下面小编就为大家带来一篇浅谈C# winForm 窗体闪烁的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网7962021-12-21
  • C#聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题,新手速来围观,一个通俗易懂的例子帮助大家更好的理解C#接口问题,感兴趣的小伙伴们可以参考一下...

    zenkey7072021-12-03
  • C#C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    这篇文章主要介绍了C#实现的文件操作封装类,结合完整实例形式分析了C#封装文件的删除,移动,复制,重命名等操作相关实现技巧,需要的朋友可以参考下...

    Rising_Sun3892021-12-28