目录

一、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

         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();
        }
    }
}
/*创建线程  */
12-11 16:24