C# Thread.Abort方法与ThreadAbortException异常(取消线程与异常处理)

1、Abort当前线程,后续程序不会执行

class Program
{
    public static Thread thread1;

    static void Main(string[] args)
    {
        thread1 = new Thread(Method1);
        thread1.Start();
        Console.ReadKey();
    }
    public static void Method1()
    {
        try
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Mthod1: " + i.ToString());
                Thread.Sleep(1000);
                if (i == 3)
                {
                    Thread.CurrentThread.Abort();   // 抛出的ThreadAbortException异常
                }
                Console.WriteLine("Mthod1: " + i.ToString() + " End");
            }
        }
        catch (SocketException ex)
        {
            Console.WriteLine("Method1 SocketException: " + ex.ToString());
        }
        catch (ThreadAbortException ex)
        {
            // ThreadAbortException要在Exception的前面,因为Exception能够匹配所有异常
            Console.WriteLine("Method1 ThreadAbortException: " + ex.ToString());
        }
        catch (Exception ex)
        {
            Console.WriteLine("Method1 Exception: " + ex.ToString());
        }
        finally
        {
            Console.WriteLine("Method1 Finally:");
        }
    }

}

执行结果:

1、thread1.Abort()执行后会直接抛出ThreadAbortException异常。

2、异常会停止后续所有程序的执行(没有输出"Method1: 3 End")。

3、catch语句会执行。catch (Exception ex)和catch (ThreadAbortException ex)都可以捕获异常,由于Exception可以匹配所有异常,因此catch (ThreadAbortException ex)应该在catch (Exception ex)的前面使用,所以输出了“"Method1 ThreadAbortException”。

4、finally语句会执行

2、Abort当前线程,进行try catch捕获异常

class Program
{
    public static Thread thread1;

    static void Main(string[] args)
    {
        thread1 = new Thread(Method1);
        thread1.Start();
        Console.ReadKey();
    }

    public static void StopMethod1()
    {
        try
        {
            thread1.Abort();                 // 首先捕获抛出的ThreadAbortException异常
        }
        catch (Exception ex)
        {
            Console.WriteLine("StopMethod1: " + ex.ToString());
        }
    }
    public static void Method1()
    {
        try
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Mthod1: " + i.ToString());
                Thread.Sleep(1000);
                if (i == 5) StopMethod1();  // 再次捕获抛出的ThreadAbortException异常
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Method1: " + ex.ToString()); 
        }
    }        
}

运行结果:

try catch{} 捕获两次抛出的ThreadAbortException。

2、Abort当前线程,不进行try catch捕获异常

class Program
{
    public static Thread thread1;

    static void Main(string[] args)
    {
        thread1 = new Thread(Method1);
        thread1.Start();
        Console.ReadKey();
    }

    public static void StopMethod1()
    {
        thread1.Abort();
    }
    public static void Method1()
    {
        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine("Mthod1: " + i.ToString());
            Thread.Sleep(1000);
            if (i == 5) StopMethod1();
        }
    }
}

运行结果:

1、虽然线程抛出了ThreadAbortException异常,由于线程中没有使用try catch的语句捕获异常,所以看上去线程很平静的退出了。

2、而且,由于是在线程中抛出的异常,所以主进程没有任何错误提示。

3、for循环只输出到5,Abort()后一般线程会直接停止。

3、在一个线程中Abort另一个线程

class Program
{
    public static Thread thread1;
    public static Thread thread2;

    static void Main(string[] args)
    {
        thread1 = new Thread(Method1);
        thread2 = new Thread(Method2);
        thread1.Start();
        thread2.Start();
        Console.ReadKey();
    }

    public static void StopMethod1()
    {
        try
        {
            thread2.Abort();
        }
        catch (Exception ex)
        {
            Console.WriteLine("StopMethod1: " + ex.ToString());
        }
    }
    public static void Method1()
    {
        try
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Method1: " + i.ToString());
                Thread.Sleep(1000);
                if (i == 5) StopMethod1();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Method3: " + ex.ToString());
        }
    }
    public static void Method2()
    {
        try
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Method2: " + i.ToString());
                Thread.Sleep(1000);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Method2: " + ex.ToString());
        }
    }
}

运行结果:

1、可以看到只在thread2中捕获到了ThreadAbortException异常。

2、thread1完整的运行完毕了,并且没有ThreadAbortException异常抛出。

总结

1、theadX.Abort()方法会在线程X中抛出ThreadAbortException异常,线程X中所有正在执行的try catch都会捕获该异常。

2、theadX.Abort()有可能在Y线程中调用的,不会在Y线程中抛出ThreadAbortException异常。

posted @ 2019-11-02 15:07  Rain Man  阅读(6386)  评论(0编辑  收藏  举报