Application.ThreadException:内存泄漏,如果不沾边?内存、不沾边、Application、ThreadException

2023-09-04 01:19:55 作者:双手成就你的梦

的reference页 Application.ThreadException说:

The reference page for Application.ThreadException says

由于这是一个静态的事件,你必须分离事件处理程序   当应用程序被处置,或者内存泄漏会导致。

Because this is a static event, you must detach your event handlers when your application is disposed, or memory leaks will result.

尽管事实上是非常的页面不分离事件处理程序对样品code,它真的泄漏,如果事件处理程序不沾边?

Notwithstanding the fact that the sample code on that very page does not detach the event handler, does it really leak if the event handler is not detached?

好像处理程序应该被分离的唯一的一次是,如果在应用程序关闭。在这种情况下,处理程序是否被分离时,所使用的所有应用程序的存储器将被反正释放

It seems the only time the handler should be detached is if the application shuts down. In that case, whether or not the handler is detached, all the memory used by the application will be freed anyway?

推荐答案

这可能是非常罕见的,但WinForms应用程序的主要()方法可能可能,出于某种原因, ,是这样的:

It is probably very uncommon, but a WinForms application's Main() method could possibly, for some reason, look like this:

static bool AbortStartup { get; set; }

[STAThread]
public static void Main()
{
    Application.Run(new CancelableSplashScreen());

    if (!AbortStartup)
        Application.Run(new MainWindow());
}

在启动画面关闭,主窗口中会出现,除非启动画面设定 AbortStatup 属性。如果从启动画面中添加一个事件处理程序 Application.ThreadException ,实例 CancelableSplashScreen 不会垃圾收集,直到应用程序终止,这可能是一个相当长的时间后。

When the splash screen closes, the main window will appear, unless the splash screen set the AbortStatup property to true. If you added an event handler to Application.ThreadException from within the splash screen, the instance of CancelableSplashScreen won't be garbage collected until the application terminates, which may be a considerable time later.

 
精彩推荐
图片推荐