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

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

服务器之家 - 编程语言 - C# - c# Thread类线程常用操作详解

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

2022-11-07 13:16UP技术控 C#

这篇文章主要介绍了c# Thread类线程常用操作详解的相关资料,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下

创建线程

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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();
    }
  }

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

?
1
2
In Main: Creating the Child thread
Child thread starts

管理线程

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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();
    }
  }

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

?
1
2
3
4
In Main: Creating the Child thread
Child thread starts
Child Thread Paused for 5 seconds
Child thread resumes

销毁线程

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

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

下面的程序说明了这点:

?
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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();
    }
  }

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

?
1
2
3
4
5
6
7
8
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类线程的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/lyl6796910/p/14517330.html

延伸 · 阅读

精彩推荐
  • C#Unity3D实现NavMesh导航网格寻路

    Unity3D实现NavMesh导航网格寻路

    这篇文章主要为大家详细介绍了Unity3D实现NavMesh导航网格寻路,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    Gary_Leong10682022-09-07
  • C#C# Winform选项卡集成窗体详解

    C# Winform选项卡集成窗体详解

    这篇文章主要为大家详细介绍了C# Winform选项卡集成窗体详解的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    绛河6252022-01-24
  • C#C#实现两个richtextbox控件滚动条同步滚动的简单方法

    C#实现两个richtextbox控件滚动条同步滚动的简单方法

    这篇文章主要给大家介绍了C#实现两个richtextbox控件滚动条同步滚动的简单方法,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面...

    鲁广广11762022-01-06
  • C#C#与C++与互操作实例讲解

    C#与C++与互操作实例讲解

    在本篇文章里小编给大家整理了关于C#与C++与互操作实例以及相关内容,需要的朋友们可以学习下。...

    zhaotianff4972022-08-04
  • C#浅谈static a[n*m]={0};中static的作用

    浅谈static a[n*m]={0};中static的作用

    下面小编就为大家带来一篇浅谈static a[n*m]={0};中static的作用。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网4292021-12-29
  • C#winform实现五子棋游戏

    winform实现五子棋游戏

    这篇文章主要为大家详细介绍了winform实现五子棋游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    Jia_ShengJie4612022-02-19
  • C#C#自定义日志记录

    C#自定义日志记录

    这篇文章主要为大家详细介绍了C#自定义日志记录的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    深入学习ing12372021-12-07
  • C#C# 获取硬件参数的实现方法

    C# 获取硬件参数的实现方法

    这篇文章主要介绍了C# 获取硬件参数的实现方法的相关资料,希望通过本文能帮助到大家,让大家实现这样的功能,需要的朋友可以参考下...

    lan_liang8192022-01-25