简体   繁体   English

为什么叫Dispose?

[英]Why is Dispose being called?

In my application, I'm seeing that at times the Dispose method on my main form gets called for apparently no reason. 在我的应用程序中,我看到有时主窗体上的Dispose方法有时显然没有理由被调用。 I'm not closing the app through the UI, I'm not sending a close windows message or calling Close() anywhere, however the Dispose method still gets called. 我不是通过UI关闭应用程序,不是在发送关闭Windows消息或在任何地方调用Close(),但是仍然会调用Dispose方法。 Here is the call stack: 这是调用堆栈:

Bitter.Shell.exe!Bitter.Shell.MainForm.Dispose(bool disposing = true) Line 853 C#
  System.dll!System.ComponentModel.Component.Dispose() + 0x12 bytes 
  System.Windows.Forms.dll!System.Windows.Forms.ApplicationContext.Dispose(bool disposing) + 0x35 bytes 
  System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.DisposeThreadWindows() + 0x33 bytes 
  System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.Dispose(bool postQuit) + 0xf8 bytes 
  System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason = -1, System.Windows.Forms.ApplicationContext context = {System.Windows.Forms.ApplicationContext}) + 0x276 bytes 
  System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) + 0x61 bytes 
  System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm) + 0x31 bytes 
  Bitter.Shell.exe!Bitter.Shell.Program.Main() Line 105 + 0x26 bytes C#

Would the CLR call it if memory was low in attempt to clean up? 如果内存不足以尝试清除,CLR会调用它吗? I know that Windows Mobile does that very thing, but didn't think that happened in the desktop world. 我知道Windows Mobile确实可以做到这一点,但是我认为这在台式机世界中没有发生。 Anyone know why this might get called? 有人知道为什么会打电话吗?

EDIT: After a reboot, I no longer see this issue. 编辑:重新启动后,我不再看到此问题。 So it seems like it was somehow due to the state of my system at the time. 因此,这似乎是由于当时系统状态所致。 Either way, the cause should still be identifiable. 无论哪种方式,原因都应该是可识别的。

是否有机会在UI线程中引发异常?

Add a breakpoint in your dispose method and follow the call stack to see what in your code is calling the dispose method. 在您的dispose方法中添加一个断点,并遵循调用堆栈,以查看代码中的内容正在调用dispose方法。 .NET does not call dispose at any time unless your application is being shutdown by the system or your program itself. 除非系统或程序本身关闭了您的应用程序,否则.NET不会随时调用dispose。

There must be an exception being thrown. 必须抛出一个异常。 Are you nesting message loops? 您是否嵌套消息循环?

Are you sure that your form isn't somehow being closed? 您确定您的表格没有以某种方式被关闭吗?

EDIT : Click Debug, Exceptions, make VS break on all managed exceptions, and see if there are any exceptions being swallowed. 编辑 :单击调试,异常,使VS在所有托管异常上中断,并查看是否有任何异常被吞噬。

A try/catch around Application.Run in Program.cs as mentioned in the comment to Jon Skeet's reply won't catch all exceptions. 如对Jon Skeet答复的评论中所述,在Program.cs中绕Application.Run进行一次尝试/捕获将无法捕获所有异常。

I would recommend you add a handler for Application.ThreadException before calling Application.Run: 我建议您在调用Application.Run之前为Application.ThreadException添加一个处理程序:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);

    try
    {
        ...
        Application.Run(new MainForm());
    }
    catch (Exception ex)
    {
        ... handle exception ...
    }
}

private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
   ... handle exception ...
}

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

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