C#暂停和恢复(Thread.Suspend()和Thread.Resume() vs AutoResetEvent()和EventWaitHandle())

本文介绍了自.NET2.0以来弃用的Thread.Suspend()和Thread.Resume()方法,推荐使用AutoResetEvent()和EventWaitHandle()进行线程同步,提供示例代码演示其功能和用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

一、Thread.Suspend()和Thread.Resume()

二、AutoResetEvent()和EventWaitHandle()

1.AutoResetEvent()

2.EventWaitHandle()

3.示例及生成效果 


一、Thread.Suspend()和Thread.Resume()

        自 .NET 2.0 以后(含),Thread.Suspend() 和 Thread.Resume() 这两个方法已过时,被VS抛弃。不被任何现在流行的VS支持使用。

        Thread.Suspend 方法 (System.Threading) | Microsoft Learn  https://learn.microsoft.com/zh-cn/dotnet/api/system.threading.thread.suspend?view=net-8.0

        Thread.Resume 方法 (System.Threading) | Microsoft Learn  https://learn.microsoft.com/zh-cn/dotnet/api/system.threading.thread.resume?view=net-8.0 

        只支持历史版本.NET Framework 1.1。.NET Framework.NET Framework

产品版本 (已过时)
.NET(Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8)
.NET Framework1.1 (2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1)
.NET Standard(2.0, 2.1)
Xamarin.iOS(10.8)
Xamarin.Mac(3.0)

         Thread.Suspend()和Thread.Resume()使用示例,可以参考作者的文章:

         C#的线程技术及操作(Thread类)-CSDN博客  https://blog.csdn.net/wenchm/article/details/134899365?spm=1001.2014.3001.5501

二、AutoResetEvent()和EventWaitHandle()

        AutoResetEvent()和EventWaitHandle() 是上述方法被废弃后的替代方法。

1.AutoResetEvent()

        表示线程同步事件在一个等待线程释放后收到信号时自动重置。 此类不能被继承。

        命名空间:System.Threading

        AutoResetEvent 类 (System.Threading) | Microsoft Learn  https://learn.microsoft.com/zh-cn/dotnet/api/system.threading.autoresetevent?view=net-8.0

2.EventWaitHandle()

         表示一个线程同步事件。

        命名空间:System.Threading 

        EventWaitHandle 类 (System.Threading) | Microsoft Learn  https://learn.microsoft.com/zh-cn/dotnet/api/system.threading.eventwaithandle?view=net-8.0

3.示例及生成效果 

// 通过实例化Thread类对象创建一个新的线。
// 最后调用Start()方法启动该线程
using System.Threading;
namespace _02_1
{
    class Program
    {
        /// <summary>
        /// 用线程起始点的ThreadStart委托创建该线程的实例
        /// </summary>
        static void Main(string[] args)
        {
            Thread myThread;									//声明线程
            myThread = new Thread(new ThreadStart(CreateThread));
            myThread.Start();									//启动线程

            /*myThread.Suspend();		*/						//挂起线程,或者如果线程已挂起,则不起作用。 CS0618
            //myThread.Resume();							    //恢复/继续已挂起的线程。CS0618
            WorkerThread();                                    //挂起线程
            StartWorking();                                     //继续已挂起的线程
        }

        public static void CreateThread()
        {
            Console.Write("创建线程");
        }

        public static EventWaitHandle wh = new AutoResetEvent(false);
        private static void WorkerThread()
        {
            while (true)
            {
                wh.WaitOne();
                //Do work.
            }
        }

        public static void StartWorking()
        {
            wh.Set();
        }
    }
}
/*创建线程  */

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wenchm

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值