么ExecuteCodeWithGuaranteedCleanu

么ExecuteCodeWithGuaranteedCleanu

本文介绍了为什么ExecuteCodeWithGuaranteedCleanup不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是想测量堆栈深度。为什么下面的程序不能打印东西吗?



 类节目
{
私有静态诠释深度= 0;

静态无效A(对象o)
{
深度++;
A(O);
}

静态无效B(对象o,布尔E)
{
Console.WriteLine(深度);
}

静态无效的主要(字串[] args)
{
RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(A,B,NULL);
}
}



一些答案​​简单地包括报价从MSDN,就像与.NET Framework 2.0版开始,一个StackOverflowException对象不能被一个try-catch块捕获和相应的过程默认情况下终止。相信我,有时(当有足够的堆栈空间),也可以毫无遗漏的,下面的打印一定数量的就好了:

 类计划
{
私有静态诠释深度= 0;

静态无效A(对象o)
{
深度++;如果
(Environment.StackTrace.Length> 8000)
抛出新StackOverflowException(如果你能赶上我。);
A(O);
}

静态无效B(对象o,布尔E)
{
Console.WriteLine(深度);
}

静态无效的主要(字串[] args)
{
RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(A,B,NULL);
}
}


解决方案

如果你要抓住它,它加载到另一个进程(调用回通过Remoting到你),并让恶棍代码执行那里。其他进程可能会终止,同时您的可能的得到一个整洁的国有企业突然跳出在你身边管道的末端 - 没有相当不方便异常的不利影响。



请注意,在同一进程中一个单独的AppDomain不会削减它。



如果您想从异常下面的代码得到的堆栈跟踪将你伟大的正义:

 类节目
{
静态无效的主要(字串[] args)
{

{
递归(0);
}
赶上(异常前)
{
堆栈跟踪ST =新的堆栈跟踪(除息);
//去野外。
Console.WriteLine(st.FrameCount);
}
到Console.ReadLine();
}

静态无效的递归(INT计数器)
{
如果(计数器> = 100)
抛出新的异常();
递归(++计数器);
}
}


I was trying to "measure" stack depth. Why the following program doesn't print anything?

class Program
{
    private static int Depth = 0;

    static void A(object o)
    {
        Depth++;
        A(o);
    }

    static void B(object o, bool e)
    {
        Console.WriteLine(Depth);
    }

    static void Main(string[] args)
    {
        RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(A, B, null);
    }
}

Some answers simply include a quote from MSDN, like "Starting with the .NET Framework version 2.0, a StackOverflowException object cannot be caught by a try-catch block and the corresponding process is terminated by default." Believe me, sometimes (when there is enough stack space) it can be cought, the following prints some number just fine:

class Program
{
    private static int depth = 0;

    static void A(object o)
    {
        depth++;
        if (Environment.StackTrace.Length > 8000)
            throw new StackOverflowException("Catch me if you can.");
        A(o);
    }

    static void B(object o, bool e)
    {
        Console.WriteLine(depth);
    }

    static void Main(string[] args)
    {
        RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(A, B, null);
    }
}
解决方案

If you want to catch it, load it into another process (that calls-back to yours via remoting) and lets the miscreant code execute there. The other process may terminate, and you could get a neat SOE popping out the end of the pipe on your side - without the adverse effects of the rather inconvenient exception.

Note that a separate AppDomain in the same process won't cut it.

If you want to get the stack trace from an exception the following code will do you great justice:

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Recurse(0);
            }
            catch (Exception ex)
            {
                StackTrace st = new StackTrace(ex);
                // Go wild.
                Console.WriteLine(st.FrameCount);
            }
            Console.ReadLine();
        }

        static void Recurse(int counter)
        {
            if (counter >= 100)
                throw new Exception();
            Recurse(++counter);
        }
    }

这篇关于为什么ExecuteCodeWithGuaranteedCleanup不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 09:11