Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么要打电话?_C#_.net_Winforms_Clr - Fatal编程技术网

C# 为什么要打电话?

C# 为什么要打电话?,c#,.net,winforms,clr,C#,.net,Winforms,Clr,在我的应用程序中,我看到有时我的主窗体上的Dispose方法被调用,显然没有任何原因。我没有通过UI关闭应用程序,也没有在任何地方发送关闭windows消息或调用close(),但是仍然会调用Dispose方法。以下是调用堆栈: Bitter.Shell.exe!Bitter.Shell.MainForm.Dispose(bool disposing = true) Line 853 C# System.dll!System.ComponentModel.Component.Dispose(

在我的应用程序中,我看到有时我的主窗体上的Dispose方法被调用,显然没有任何原因。我没有通过UI关闭应用程序,也没有在任何地方发送关闭windows消息或调用close(),但是仍然会调用Dispose方法。以下是调用堆栈:

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#
如果试图清理时内存不足,CLR会调用它吗?我知道Windows Mobile做的正是这件事,但我不认为这发生在桌面世界。有人知道为什么会打这个电话吗


编辑:重新启动后,我不再看到此问题。所以这似乎是由于当时我的系统的状态。无论哪种方式,原因都应该是可识别的。

是否有可能在UI线程中引发异常?

在dispose方法中添加一个断点,然后按照调用堆栈查看代码中调用dispose方法的内容。NET在任何时候都不会调用dispose,除非系统或程序本身正在关闭应用程序


必须抛出异常。是否嵌套消息循环?

是否确定表单没有以某种方式关闭


编辑:在所有托管异常上单击调试、异常、接通与断开,查看是否有任何异常被吞没。

一个try/catch-around应用程序。Jon Skeet回复中的注释中提到的在Program.cs中运行不会捕获所有异常

我建议您在调用Application.ThreadException之前添加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 ...
}

Program.Main的内容无关紧要-这是来自Application.Run.Yes,但我有一个try/catch-around应用程序。在Program.Main.Run内部运行。你发现了吗?我不相信。我在Program.cs中有一个try-catch块,它可以捕获其他地方没有捕获到的任何东西,而它通常会捕获这些东西。但是在调用Dispose之后,它就会捕获它。。。如果您在Dispose中闯入调试器,调试器将有机会捕获它。