C# 属性

C# 抽象

C# 字符串

C# 泛型

C# 杂项

C# 新特性

C# Threading Example: Abort() method 线程示例:Abort() 方法

Abort() 方法用于终止线程。如果 Abort 操作未完成,它会引发 ThreadAbortException。

例子 (Example)

using System;  
using System.Threading;  
public class MyThread  
{  
    public void Thread1()  
    {  
        for (int i = 0; i < 10; i++)  
        {  
            Console.WriteLine(i);  
            Thread.Sleep(200);  
        }  
    }  
}  
public class ThreadExample  
{  
    public static void Main()  
    {  
        Console.WriteLine("Start of Main");  
        MyThread mt = new MyThread();  
        Thread t1 = new Thread(new ThreadStart(mt.Thread1));  
        Thread t2 = new Thread(new ThreadStart(mt.Thread1));  
  
        t1.Start();  
        t2.Start();  
        try  
        {  
            t1.Abort();  
            t2.Abort();  
        }  
        catch (ThreadAbortException tae)  
        {  
            Console.WriteLine(tae.ToString());  
        }  
        Console.WriteLine("End of Main");  
    }  
}

输出:

输出是不可预测的,因为线程可能处于运行状态。

Start of Main
0
End of Main


  • 使用社交账号登录,本站支持
全部评论(0)