螺纹。C# 中的重置端口方法
原文:https://www . geesforgeks . org/thread-resetabort-method-in-c-sharp/
一个线程类负责在多线程编程中创建和管理一个线程。它提供了一种称为重置端口的方法,负责取消当前线程的中止请求。它防止线程异常终止线程。
语法:
public static void ResetAbort ();
异常:
- 线程状态异常:如果当前线程没有调用中止。
- 安全异常:如果调用者没有当前线程所需的安全权限。
以下程序说明了 ResetAbort() 方法的使用:
例 1:
// C# program to illustrate the
// use of ResetAbort method
using System;
using System.Threading;
using System.Security.Permissions;
class MyThread {
// Method Job
public void Job()
{
try {
for (int I = 0; I < 10; I++)
{
Console.WriteLine(" My Thread is working..!");
Thread.Sleep(100);
}
}
catch (ThreadAbortException e)
{
Console.WriteLine("Caught ThreadAbortException and reset");
Console.WriteLine("Ex message: {0}", e.Message);
Thread.ResetAbort();
}
Console.WriteLine("Thread is alive and working..!");
Thread.Sleep(2000);
Console.WriteLine("Thread is finished its working..!");
}
}
// Driver Class
class GFG {
// Main Method
public static void Main()
{
MyThread obj = new MyThread();
Thread T = new Thread(obj.Job);
T.Start();
Thread.Sleep(100);
Console.WriteLine("Aborting thread");
T.Abort();
T.Join();
Console.WriteLine("Main thread ends");
}
}
输出:
My Thread is working..!
Aborting thread
Caught ThreadAbortException and reset
Ex message: Thread was being aborted.
Thread is alive and working..!
Thread is finished its working..!
Main thread ends
例 2:
// C# program to illustrate the
// use of ResetAbort method
using System;
using System.Threading;
// Driver Class
public class GFG {
// Main Method
public static void Main()
{
// Creating and initializing threads
Thread thr = new Thread(Job);
// Start the execution of Thread
thr.Start();
// Reset abort request
// Using ResetAbort method
Thread.ResetAbort();
}
public static void Job()
{
Console.WriteLine("Hello");
}
}
运行时错误:
未处理异常: 系统。线程。线程状态异常:无法重置中止,因为没有请求中止。
参考:
版权属于:月萌API www.moonapi.com,转载请注明出处