Having read this article (and others) on .NET reliability features, I am led to believe that the following code should handle StackOverflowExceptions:
class Program
{
private static int s_exitCode = -1;
static int Main(string[] args)
{
RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(DoStuff, Cleanup, null);
return s_exitCode;
}
[MethodImpl(MethodImplOptions.NoInlining)]
static void DoStuff(object userdata)
{
DoStuff(userdata);
}
[MethodImpl(MethodImplOptions.NoInlining)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
static void Cleanup(object userData, bool exceptionThrown)
{
s_exitCode = 0;
}
}
I have sited the above code in a console application and have also included the following entry in the app's config file:
<runtime>
<legacyUnhandledExceptionPolicy enabled="1" />
</runtime>
I start it via a Process.Start() call from another application. However, in spite of all this, the process crashes, returning an exit code of -2147023895. And despite a lot of research and tinkering, I am no closer to finding out how to get this to work.
Any advice gratefully received, thanks.
Chris.