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

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

服务器之家 - 编程语言 - C# - C# 中如何使用Thread

C# 中如何使用Thread

2022-10-31 11:22一线码农 C#

这篇文章主要介绍了C# 中使用 Thread的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下

线程是进程中的最小执行单元,多线程是指在给定时间内拥有多个线程的能力,并且可以调度它们从而在某一时刻处理多个操作,微软的 .Net Framework 提供了 Thread 来帮助我们完成多线程开发。

Thread 编程

要想使用 Thread,需要在程序中引用 System.Threading 命名空间,然后再提供一个供线程调度的方法,这个方法是通过 Thread 中的 ThreadStart 委托代理的,下面的代码展示了如何创建线程。

?
1
Thread t = new Thread(new ThreadStart(MyThreadMethod));

线程创建好之后,还需要调用 Start 方法去启动,下面的代码展示了如何去实现,哦,对了,上面的 MyThreadMethod方法会在新的线程上被调度,而不是调用线程。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
class Program
{
  static void Main(string[] args)
  {
    Thread t = new Thread(new ThreadStart(MyThreadMethod));
    t.Start();
    Console.Read();
  }
  static void MyThreadMethod()
  {
    Console.WriteLine("Hello World!");
  }
}

展示线程状态

一个创建好的线程,它的生命周期内会有多个状态,比如:Aborted, Background, Running, Stopped, Suspended, Unstarted 等等,这些状态在 Thread 中是用 ThreadState 枚举表示的,如下代码所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Flags]
public enum ThreadState
{
  Running = 0,
  StopRequested = 1,
  SuspendRequested = 2,
  Background = 4,
  Unstarted = 8,
  Stopped = 16,
  WaitSleepJoin = 32,
  Suspended = 64,
  AbortRequested = 128,
  Aborted = 256
}

当一个 Thread 对象创建好之后,它的状态就是 Unstarted,然而当 Start 方法启动之后,线程的状态将会从 Unstarted 切换到 Running 状态,下面的代码展示了这种轮转。

?
1
2
3
4
5
6
7
static void Main(string[] args)
{
  Thread t = new Thread(new ThreadStart(MyThreadMethod));
  Console.WriteLine("The thread's state is:" + t.ThreadState.ToString());
  t.Start();
  Console.WriteLine("The thread's state is:" + t.ThreadState.ToString());
}

C# 中如何使用Thread

控制线程的 前台和后台

一个线程要么是前台线程要么是后台线程,如果你是通过显式的方式创建线程,它便是前台线程,前后线程最大的区别在于:应用程序退出的前提必须是程序内的所有前台线程都得到退出,相反,应用程序的退出不依赖于后台线程。

你可以通过 IsBackground 属性来设置 Thread 的前台或者后台,下面的代码展示了如何去实现。

?
1
2
3
4
5
6
7
8
static void Main(string[] args)
{
  Thread t = new Thread(new ThreadStart(MyThreadMethod));
  t.Start();
  t.IsBackground = true;
  Console.WriteLine(“The thread's background status is: “+t.IsBackground.ToString());
  Console.Read();
}

除了启动线程,还可以通过 Suspend() 和 Resume() 方法来 挂起 和 恢复 线程, 值得注意的是,你只能 恢复 你之前通过 Suspend 方法 挂起的线程,如下代码所示:

?
1
2
3
4
5
6
7
8
static void Main(string[] args)
{
  Thread t = new Thread(new ThreadStart(MyThreadMethod));
  t.Start();
  t.Suspend(); //Suspends the newly created thread
  t.Resume(); //Resumes the suspended thread
  Console.Read();
}

值得注意的是,现在的 Thread.Suspend() Thread.Resume() 方法都是被标记成弃用的状态了,取而代之的做法是:使用 AutoResetEventEventWaitHandle 方法来实现多线程之间的同步。

设置线程优先级

可以给一个线程赋予优先级,从而和内存中的其他线程争抢 CPU 时间,在 C# 中是使用 ThreadPriority 枚举来表示,大体上有如下值: Lowest, BelowNormal, Normal, AboveNormal 和 Highest,下面的代码展示了如何给这两个线程赋予优先级。

?
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
class Program
{
  static void Main(string[] args)
  {
    Thread thread1 = new Thread(new ThreadStart(Method1));
    Thread thread2 = new Thread(new ThreadStart(Method2));
 
    thread1.Priority = ThreadPriority.Highest;
    thread2.Priority = ThreadPriority.Lowest;
 
    thread2.Start();
    thread1.Start();
 
    Console.Read();
  }
  static void Method1()
  {
    for (int i = 0; i < 10; i++)
    {
      Console.WriteLine("First thread: " + i);
    }
  }
  static void Method2()
  {
    for (int i = 0; i < 10; i++)
    {
      Console.WriteLine("Second thread: " + i);
    }
  }
}

C# 中如何使用Thread

从上面的输出结果中可以看出,Thread1 先于 Thread2 执行完,即使 Thread2.Start 是先启动的,是不是很好的演示了优先级的概念。

线程是昂贵的,因为线程的整个生命周期需要消耗太多的资源,比如:初始化,上下文切换,释放使用的资源 等等,所以在用 多线程 之前需要想好是否真的要这么做,当用多线程的时候,适当的使用 线程池 (ThreadPool) 是一个非常好的做法,毕竟线程池内部会帮你自动创建,释放,调度线程,你只需要傻傻的用即可,同时也是提升程序响应的利器。

译文链接:https://www.infoworld.com/article/3035134/how-to-work-with-threads-in-c.html

以上就是C# 中如何使用Thread的详细内容,更多关于c# 使用 Thread的资料请关注服务器之家其它相关文章!

原文链接:https://juejin.cn/post/6921949414130991112

延伸 · 阅读

精彩推荐
  • C#C#贪吃蛇游戏实现分析

    C#贪吃蛇游戏实现分析

    这篇文章主要为大家分析了C#贪吃蛇游戏的实现代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    冰封一夏10342022-01-06
  • C#C#读写INI文件的方法

    C#读写INI文件的方法

    这篇文章主要介绍了C#读写INI文件的方法,涉及C#读写ini文件的相关实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    Nozer3982021-10-28
  • C#C#利用栈实现加减乘除运算

    C#利用栈实现加减乘除运算

    这篇文章主要介绍了C#利用栈实现加减乘除运算的实现方法,需要的朋友可以参考下...

    Don_Yao10422022-08-08
  • C#C#基础知识之base关键字介绍

    C#基础知识之base关键字介绍

    本文主要介绍base关键字的使用方法,base关键字可以调用基类重写的方法,可以调用基类的构造方法,还可以在EntityFramework中使用,下面一一介绍。...

    Swich6872021-11-19
  • C#使用C# 的webBrowser写模拟器时的javascript脚本调用问题

    使用C# 的webBrowser写模拟器时的javascript脚本调用问题

    这篇文章主要介绍了C# 的webBrowser写模拟器时的javascript脚本调用问题,需要的朋友可以参考下...

    jackxinxu21009222022-01-17
  • C#C#二维码图片识别代码

    C#二维码图片识别代码

    这篇文章主要为大家详细介绍了C#二维码图片识别代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    志在必得Shaun10112022-02-24
  • C#细说C#中的枚举:转换、标志和属性

    细说C#中的枚举:转换、标志和属性

    枚举是 C# 中最有意思的一部分,大部分开发人员只了解其中的一小部分,甚至网上绝大多数的教程也只讲解了枚举的一部分。那么,我将通过这篇文章向大...

    朱钢11532022-08-27
  • C#c# 判断是否为空然后赋值的4种实现方法

    c# 判断是否为空然后赋值的4种实现方法

    下面小编就为大家分享一篇c# 判断是否为空然后赋值的4种实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    杨明波(Leo Yang)7122022-02-16