当前位置: 移动技术网 > IT编程>开发语言>c# > C#多线程之Thread类详解

C#多线程之Thread类详解

2019年07月18日  | 移动技术网IT编程  | 我要评论

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

常用的构造函数有:

 // 摘要: 
  //  初始化 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的无参数方法。

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");
  }

输出结果:

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

 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;另一种是创建一个自定义类,把线程的方法定义为实例方法,这样先初始化实例的数据,在启动线程。

如:传递参数

public struct tddata // 传递数据
  {
   public string message; //数据string字段
  }

使用第一种方式:

 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);   
  }

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

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()作为构造函数的参数。

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属性,来让其成为后台线程;

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#多线程之线程控制详解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网