Czytałem o Reliability Features in .NET i napisałem następujące klasy zbadać ExecuteCodeWithGuaranteedCleanup
Kiedy program ExecuteCodeWithGuaranteedCleanup faktycznie gwarantuje czyszczenie?
class Failing
{
public void Fail()
{
RuntimeHelpers.PrepareConstrainedRegions();
try
{
}
finally
{
RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(Code, Cleanup, "fail");
}
}
private void Code(object message)
{
// Some code in here that will cause an exception...
}
private void Cleanup(object message, bool something)
{
Console.WriteLine(message);
Console.ReadLine();
}
}
I eksperymentowali z różnymi organami kod na metodzie Code
. Te, a ich wyniki runtime są wymienione poniżej
powodując OutOfMemoryException
- Cleanup
nie sprawdzony
List<string> ss = new List<string>();
while (true)
{
string s = new string('x', 1000000);
ss.Add(s);
}
powodując StackOverflowException
- Cleanup
nie sprawdzony
Code(message); // recursive call
Spowodowanie a ExecutionEngineException
- Cleanup
nie sprawdzony
Environment.FailFast(message.ToString());
Spowodowanie ThreadAbortException
- Cleanup
robi sprawdzony (jednak regularne try...finally
można również złapać ten wyjątek)
Thread.CurrentThread.Abort();
więc pytania są
- Czy poprawnie używam
ExecuteCodeWithGuaranteedCleanup
? - Kiedy jest
ExecuteCodeWithGuaranteedCleanup
rzeczywiście przydatne?
Uruchom ten kod na hoście CLR, który implementuje ICLRPolicyManager. SQL Server. –