I read about Reliability Functions in .NET and wrote the following class to learn ExecuteCodeWithGuaranteedCleanup
class Failing { public void Fail() { RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(Code, Cleanup, "fail"); } } private void Code(object message) {
I experimented with various code bodies for the Code method. These and their results are indicated below.
Causing OutOfMemoryException - Cleanup does not raise
List<string> ss = new List<string>(); while (true) { string s = new string('x', 1000000); ss.Add(s); }
Causing StackOverflowException - Cleanup Does Not Call
Code(message);
Causing ExecutionEngineException - Cleanup Does Not Call
Environment.FailFast(message.ToString());
Raises a call to ThreadAbortException - Cleanup does (however a regular try...finally can also catch this exception)
Thread.CurrentThread.Abort();
So the questions are:
- Am I using
ExecuteCodeWithGuaranteedCleanup ? - When is
ExecuteCodeWithGuaranteedCleanup really useful?
Richard Everett
source share