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

 更新时间:2021年03月12日 08:55:24   作者:UP技术控  
这篇文章主要介绍了c# Thread类线程常用操作详解的相关资料,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下

创建线程

线程是通过扩展 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

以上就是c# Thread类线程常用操作详解的详细内容,更多关于c# Thread类线程的资料请关注脚本之家其它相关文章!

相关文章

  • 一文带你了解C#中的协变与逆变

    一文带你了解C#中的协变与逆变

    这篇文章介绍了C#中协变和逆变的相关知识,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-02-02
  • C#中 城市线路图的纯算法以及附带求极权值

    C#中 城市线路图的纯算法以及附带求极权值

    本篇文章介绍了,在C#中城市线路图的纯算法以及附带求极权值的方法,需要的朋友参考下
    2013-04-04
  • C#关键字之覆写overwrite介绍

    C#关键字之覆写overwrite介绍

    这篇文章介绍了C#关键字之覆写overwrite,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • C#实现读取txt文件生成Word文档

    C#实现读取txt文件生成Word文档

    大家好,本篇文章主要讲的是C#实现读取txt文件生成Word文档,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-01-01
  • 使用C#自制一个Windows安装包的详细过程

    使用C#自制一个Windows安装包的详细过程

    这篇文章主要介绍了如何使用C#自制一个Windows安装包,文中通过图文结合的方式给大家介绍的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下
    2024-07-07
  • WPF使用webView实现显示浏览器网页

    WPF使用webView实现显示浏览器网页

    在WPF中显示一个可以操作的浏览器界面,你可以使用WebBrowser控件或WebView2控件,下面我们就来看看如何分别使用这两个控件实现显示浏览器网页吧
    2025-01-01
  • C#实现IP摄像头的方法

    C#实现IP摄像头的方法

    这篇文章主要介绍了C#实现IP摄像头的方法,涉及C#IP连接与摄像头视频录像的相关技巧,需要的朋友可以参考下
    2015-04-04
  • 详解C#如何使用消息队列MSMQ

    详解C#如何使用消息队列MSMQ

    消息队列 (MSMQ Microsoft Message Queuing)是MS提供的服务,也就是Windows操作系统的功能,下面就跟随小编一起了解一下C#中是如何使用消息队列MSMQ的吧
    2024-01-01
  • C#配置文件操作类分享

    C#配置文件操作类分享

    这篇文章主要分享了C#配置文件操作类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • C#实现json格式数据解析功能的方法详解

    C#实现json格式数据解析功能的方法详解

    这篇文章主要介绍了C#实现json格式数据解析功能的方法,结合实例形式较为详细的分析了C#解析json格式数据的具体操作步骤与相关注意事项,需要的朋友可以参考下
    2017-12-12

最新评论