在VB.NET控制台应用程序关闭功能控制台、应用程序、功能、VB

由网友(纯属灬娱乐丿)分享简介:我怎么能火了一个功能,它在使用检测控制台收盘时 Environment.Exit(0)?How can I fire off a function when it detects the console closing when using Environment.Exit(0)?推荐答案这样做的最简单的方法可能...

我怎么能火了一个功能,它在使用检测控制台收盘时 Environment.Exit(0)

How can I fire off a function when it detects the console closing when using Environment.Exit(0)?

推荐答案

这样做的最简单的方法可能是处理的AppDomain.ProcessExit事件,这是提高在应用程序的父进程退出。

The simplest way of doing this is probably to handle the AppDomain.ProcessExit event, which is raised when the application's parent process exits.

例如:

Module MyApp

    Sub Main()
        ' Attach the event handler method
        AddHandler AppDomain.CurrentDomain.ProcessExit, AddressOf MyApp_ProcessExit

        ' Do something
        ' ...

        Environment.Exit(0)
    End Sub

    Private Sub MyApp_ProcessExit(sender As Object, e As EventArgs)
        Console.WriteLine("App Is Exiting...")
    End Sub

End Module

但调用 Environment.Exit 可能不是你原来的问题的最佳解决方案。在一般情况下,唯一时间,有必要用这种方法是,当有可能是运行的其它前台线程。而在这种情况下,这是值得研究的正常终止的其他线程的方式而不是诉诸严厉的措施,杀死的全过程。

But calling Environment.Exit may not be the best solution to your original problem. In general, the only time it is necessary to use this method is when there might be other foreground threads running. And in that case, it's worth investigating ways of gracefully terminating those other threads without resorting to draconian measures that kill the entire process.

Environment.Exit ,尽管有些悦耳动听的名字,是pretty的残酷手段。这不是那么糟糕单击Windows任务管理器结束任务(请注意,如果你的是的,在 ProcessExit 事件将不会待提高,这意味着上述建议将无法正常工作),但它可能不是你真正想要的解决方案,无论是。

Environment.Exit, despite the somewhat pleasant-sounding name, is a pretty brutal measure. It's not quite as bad as clicking "End Task" in the Windows Task Manager (and note that if you do that, the ProcessExit event will not be raised, meaning that the above suggestion will not work), but it's probably not the solution you really want, either.

阅读全文

相关推荐

最新文章