Partager via


.NET Trivia

Here’s a fun piece of .NET trivia involving thread aborts and exception handling. What does the following code do? And why does it do it?

 

using System;

using System.Threading;

 

class TATest

{

        public static void Main(string []args)

        {

                try

                {

                        Console.WriteLine("Aborting!");

                        Thread.CurrentThread.Abort();

                }

                finally

                {

                        if((Thread.CurrentThread.ThreadState & ThreadState.AbortRequested)!=0)

                        {

                                Console.WriteLine("Aborted!");

                                Thread.ResetAbort();

                        }

                }

                Console.WriteLine(“Bye bye!“);  

        }

}

 

(In writing this I've discovered my fingers really prefer to type byte over bye... that t just seems so natural to come next).b

Comments