"Alvin Bruney" <vapor at steaming post office> wrote in message news:%
[email protected]...
The finally block is guaranteed to be called.
There are very few exceptions
to this rule. Thread aborts aren't one of them.
Finally block *is* called.
It is just not finished, since exception is thrown in the middle of it.
Do the experiment, as I suggested. Don't blindly believe the guarantees.
This program illustrates my point:
//-------------------------------------------------------------------------
using System;
using System.Threading;
namespace AbortThreadWhileInFinallyBlock
{
class Program
{
static int Indicator = 0;
static void MyThread()
{
try
{
Console.WriteLine("In try");
}
finally
{
Console.WriteLine("Start finally");
Thread.Sleep(10000);
Indicator = 1;
Console.WriteLine("End finally");
}
}
static void Main(string[] args)
{
Thread T = new Thread(new ThreadStart(MyThread));
T.Start();
Thread.Sleep(2000);
Console.WriteLine("Aborting the thread...");
T.Abort();
Console.WriteLine("Done aborting the thread. Indicator is {0}", Indicator);
}
}
}
//-------------------------------------------------------------------------
On my machine it prints this:
In try
Start finally
Aborting the thread...
Done aborting the thread. Indicator is 0
As you can see, "end finally" was not printed, and indicator value
was not set to one. The guarantee did not work.
Ivan