简体   繁体   English

为什么在调试器中使用不调用 dispose 方法

[英]Why using does not call dispose method in debugger

I have this test code.我有这个测试代码。 Why is the dispose method not called when an exception is raised inside using statement?为什么在 using 语句中引发异常时不调用 dispose 方法? According to the documentation it should be called. 根据文档,它应该被调用。

using System;
using System.IO;
using System.Text;

namespace UsingTest {
    class Program {
    public class MyClass : IDisposable
    {
        private bool disposed = false;

        public void Dispose() {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        public void Dispose(bool disposing) {
            if (!disposed) {
                if (disposing) {
                    using (var f = new FileStream("log.txt", FileMode.Create)) {
                        var msg = "MyClass disposed";
                        f.Write(Encoding.UTF8.GetBytes(msg), 0, Encoding.UTF8.GetByteCount(msg));
                    }
                }
                disposed = true;
            }
        }

        ~MyClass() {
            Dispose(false);
        }
    }

    static void Main(string[] args) {
        using (var c = new MyClass()) {
            Console.WriteLine("some work");
            throw new Exception("Exception");
            Console.WriteLine("Hello World!");
        }
    }
}
}

Thanks.谢谢。

static void Main(string[] args) {

        using (var c = new MyClass()) {
            Console.WriteLine("some work");
            throw new Exception("Exception");
            Console.WriteLine("Hello World!");
        }

    }

This is creating a new instance of MyClass这是创建MyClass的新实例
While it's instantiated, it writes to the console.当它被实例化时,它会写入控制台。 Then throws an exception.然后抛出异常。 On exception, the debugger steps in, and tries to help you out debugging the code.在例外情况下,调试器会介入并尝试帮助您调试代码。 The debugger in Visual Studio will keep running your code long enough to do that - debug it. Visual Studio 中的调试器将继续运行您的代码足够长的时间来执行此操作 - 调试它。

if you will run it from Visual Studio with debugger - it will not be called

The method (Dispose) is being called after the debugger stops running / listening to your code.在调试器停止运行/侦听您的代码,将调用方法 (Dispose)。

Your breakpoints in Dispose won't be hit, since the debugger is no longer there.你在Dispose断点不会被击中,因为调试器不再存在。 The code isn't being run, since the runtime has exited (by order of Visual Studio determining you're now finished)代码未运行,因为运行时已退出(按 Visual Studio 的顺序确定您现在已完成)

If you run it without debugging (ctrl+f5) you will see the file created too.如果你在没有调试的情况下运行它 (ctrl+f5),你也会看到创建的文件。

Try putting a System.Diagnostics.Debugger.Launch();尝试放置一个System.Diagnostics.Debugger.Launch(); inside your Dispose method, like this:在你的 Dispose 方法中,像这样:

public void Dispose(bool disposing) {
    System.Diagnostics.Debugger.Launch();
    //etc
}

This will attach the debugger at that point in time.这将在那个时间点附加调试器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM