目錄
一、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 Framework | 1.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(); //恢復/繼續已掛起的線程。CS0618WorkerThread(); //掛起線程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();}}
}
/*創建線程 */