c# Thread类线程常用操作详解

作者:UP技术控 时间:2021-09-20 21:35:51 

目录
  • 创建线程

  • 管理线程

  • 销毁线程

创建线程

线程是通过扩展 Thread 类创建的。扩展的 Thread 类调用 Start() 方法来开始子线程的执行。

下面的程序演示了这个概念:


class ThreadCreationProgram
 {
   public static void CallToChildThread()
   {
     Console.WriteLine("Child thread starts");
   }

static void Main(string[] args)
   {
     ThreadStart childref = new ThreadStart(CallToChildThread);
     Console.WriteLine("In Main: Creating the Child thread");
     Thread childThread = new Thread(childref);
     childThread.Start();
     Console.ReadKey();
   }
 }

当上面的代码被编译和执行时,它会产生下列结果:


In Main: Creating the Child thread
Child thread starts

管理线程

Thread 类提供了各种管理线程的方法。

下面的实例演示了 sleep() 方法的使用,用于在一个特定的时间暂停线程。


class ThreadCreationProgram
 {
   public static void CallToChildThread()
   {
     Console.WriteLine("Child thread starts");
     // 线程暂停 5000 毫秒
     int sleepfor = 5000;
     Console.WriteLine("Child Thread Paused for {0} seconds",
              sleepfor / 1000);
     Thread.Sleep(sleepfor);
     Console.WriteLine("Child thread resumes");
   }

static void Main(string[] args)
   {
     ThreadStart childref = new ThreadStart(CallToChildThread);
     Console.WriteLine("In Main: Creating the Child thread");
     Thread childThread = new Thread(childref);
     childThread.Start();
     Console.ReadKey();
   }
 }

当上面的代码被编译和执行时,它会产生下列结果:


In Main: Creating the Child thread
Child thread starts
Child Thread Paused for 5 seconds
Child thread resumes

销毁线程

Abort() 方法用于销毁线程。

通过抛出 threadabortexception 在运行时中止线程。这个异常不能被捕获,如果有 finally 块,控制会被送至 finally 块。

下面的程序说明了这点:


class ThreadCreationProgram
 {
   public static void CallToChildThread()
   {
     try
     {

Console.WriteLine("Child thread starts");
       // 计数到 10
       for (int counter = 0; counter <= 10; counter++)
       {
         Thread.Sleep(500);
         Console.WriteLine(counter);
       }
       Console.WriteLine("Child Thread Completed");

}
     catch (ThreadAbortException e)
     {
       Console.WriteLine("Thread Abort Exception");
     }
     finally
     {
       Console.WriteLine("Couldn't catch the Thread Exception");
     }

}

static void Main(string[] args)
   {
     ThreadStart childref = new ThreadStart(CallToChildThread);
     Console.WriteLine("In Main: Creating the Child thread");
     Thread childThread = new Thread(childref);
     childThread.Start();
     // 停止主线程一段时间
     Thread.Sleep(2000);
     // 现在中止子线程
     Console.WriteLine("In Main: Aborting the Child thread");
     childThread.Abort();
     Console.ReadKey();
   }
 }

当上面的代码被编译和执行时,它会产生下列结果:


In Main: Creating the Child thread
Child thread starts
0
1
2
In Main: Aborting the Child thread
Thread Abort Exception
Couldn't catch the Thread Exception

来源:https://www.cnblogs.com/lyl6796910/p/14517330.html

标签:c#,Thread,线程
0
投稿

猜你喜欢

  • Java实现两人五子棋游戏(五) 判断是否有一方胜出

    2022-03-03 18:29:44
  • Java二维数组查找功能代码实现

    2023-01-04 19:47:17
  • 基于JavaMail API收发邮件的方法

    2022-03-10 09:34:24
  • Java8 使用工厂方法supplyAsync创建CompletableFuture实例

    2023-02-14 03:57:22
  • java实现字符串四则运算公式解析工具类的方法

    2021-11-03 09:22:23
  • Java实现的微信图片处理工具类【裁剪,合并,等比例缩放等】

    2022-03-26 11:32:59
  • Windows编写jar启动脚本和关闭脚本的操作方法

    2021-05-28 04:36:58
  • MyBatis-Plus多表联查(动态查询)的项目实践

    2023-11-19 21:43:17
  • 教你快速搭建sona服务及idea使用sona的方法

    2023-11-20 05:22:53
  • SpringCloudAlibaba整合Feign实现远程HTTP调用的简单示例

    2023-11-19 16:16:05
  • Visual Studio 2022 安装低版本 .Net Framework的图文教程

    2023-06-22 19:18:44
  • IDEA最新版2020.1的maven工程本地依赖仓库无法使用问题(已解决)

    2023-09-21 17:57:00
  • java注解的全面分析

    2023-11-25 11:28:50
  • java 解决异常 2 字节的 UTF-8 序列的字节2 无效的问题

    2021-06-01 10:29:00
  • 详解二维码生成工厂

    2022-09-26 11:26:13
  • C#仿QQ聊天窗口

    2022-09-30 09:12:39
  • Java网络编程实现的简单端口扫描器示例

    2022-04-17 09:59:18
  • java随机生成8位数授权码的实例

    2022-04-24 12:03:47
  • Spring MVC的优点与核心接口_动力节点Java学院整理

    2023-11-28 05:43:36
  • Android如何让WebView中的HTML5页面实现视频全屏播放

    2023-07-29 00:32:06
  • asp之家 软件编程 m.aspxhome.com