Re: Why finally?
- From: "Alvin Bruney" <www.lulu.com/owc>
- Date: Sun, 16 Apr 2006 16:53:12 -0400
Yes, very nice. I get the same results from executing the code as well.
This seems to be a definite improvement of V1.x of the framework.
I did make a minor adjustment inside the try block of threadproc to throw an
exception. I did this to test to see if the finally block would hold for
normal exception paths (and to see if I could break the guarantee as well).
//inject an exception
for (int i = 0; i < 2; i++)
i = i % i;
After displaying the exception in a dialog box, it did fire the finally
block to release the resource.
--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]
[Shameless Author plug]
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Professional VSTO.NET - Wrox/Wiley 2006
-------------------------------------------------------
"Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx> wrote in message
news:usqTkHUYGHA.4864@xxxxxxxxxxxxxxxxxxxxxxx
By 'asynchronous' I mean that the exception is not initiated by an
explicit
call to 'throw' in the executing code path (a thread).
Rather, it results from a memory allocation fault (OOM) or an injected
failure like Abort that can happen on any instruction in another thread
(or
threads see: OOM). I don't consider StackOverFlow to be asynchronous, a
StackOverflow can't be injected in another thread. Note also that I'm not
talking about CER in V2 either, I'm just talking about a feature of the
CLR,
that guarantees that the finally block will execute in the presence of an
asynchronous exception (Abort, OOM).
To see what I mean, consider this simple case, where an Abort is injected
in
another thread:
class Tester
{
static void Main()
{
Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();
Thread.Sleep(150);
// Inject an asynchronous ThreadAbortException in the target thread,
// The choice of the sleep times guarantees that the exception occurs
while
executing the finally backout code
t.Abort();
Console.WriteLine("Target Thread aborted");
}
static void ThreadProc()
{
IntPtr somePtr = IntPtr.Zero;;
try
{
somePtr = Marshal.AllocHGlobal(2000000);
Thread.Sleep(100);
}
finally
{
Thread.Sleep(200);
Marshal.FreeHGlobal(somePtr);
Console.WriteLine("Resource freed");
}
}
}
}
When running this on V2 of the framework, the CLR will prevent the
asynchronous abort (ThreadAbort or OOM) to be injected while running the
finally block. This means that the allocated resource will be freed
despite
the presence of an asychronous exception.
However, when you run this code on V1.X, the finally will not run to an
end
and the resource will leak.
Willy.
"Alvin Bruney" <www.lulu.com/owc> wrote in message
news:%23Zl6$JOYGHA.3496@xxxxxxxxxxxxxxxxxxxxxxx
| That guarantee needs qualifications right? A finally block is not
expected
| to execute when the CLR throws a stack exception or an execution engine
| exception. I take it that these form part of the qualification to the
| *guarantee
|
| --
| ________________________
| Warm regards,
| Alvin Bruney [MVP ASP.NET]
|
| [Shameless Author plug]
| The O.W.C. Black Book with .NET
| www.lulu.com/owc, Amazon
| Professional VSTO.NET - Wrox/Wiley 2006
| -------------------------------------------------------
|
| "Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx> wrote in message
| news:%23688MEMYGHA.128@xxxxxxxxxxxxxxxxxxxxxxx
| >
| > "Jon Davis" <jon@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
| > news:uuTSmZFYGHA.3972@xxxxxxxxxxxxxxxxxxxxxxx
| > |I understand that the finally sub-block will execute regardless of
| > whether
| > | try succeeded or not. But so will code that follows try...catch. So
| > then
| > | what is the difference between ...
| > |
| > | try {
| > | do.something.that.breaks();
| > | } catch {
| > | do.something.that.works();
| > | }
| > | cleanup();
| > |
| > |
| > | .. and ..
| > |
| > | try {
| > | do.something.that.breaks();
| > | } catch {
| > | do.something.that.works();
| > | } finally {
| > | cleanup();
| > | }
| > |
| > | .. other than organizational aesthetics ??
| > |
| > | Thanks,
| > | Jon
| > |
| > |
| >
| > In V2 of the framework, the finally block is guaranteed to run to
| > completion, even in the presence of an asynchronous exception (think
| > ThreadAbortExceptions).
| >
| > Willy.
| >
| >
|
|
.
- Follow-Ups:
- Re: Why finally?
- From: Willy Denoyette [MVP]
- Re: Why finally?
- References:
- Why finally?
- From: Jon Davis
- Re: Why finally?
- From: Willy Denoyette [MVP]
- Re: Why finally?
- From: Alvin Bruney
- Re: Why finally?
- From: Willy Denoyette [MVP]
- Why finally?
- Prev by Date: Re: Does WinFX need XP?
- Next by Date: Re: how to control application context?
- Previous by thread: Re: Why finally?
- Next by thread: Re: Why finally?
- Index(es):
Relevant Pages
|