vb.net 如何完全停止运行代码并退出应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13594234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:28:51  来源:igfitidea点击:

How to completely stop running the code and exit the application?

vb.netvisual-studio-2010

提问by Scott

In my code, I check if the executable file op.exeis in the application run path directory, in case if not, it does show MsgBoxand exits the application, the problem is that it doesn't really exit the application, because the code after is executing everytime.

在我的代码中,我检查可执行文件op.exe是否在应用程序运行路径目录中,如果没有,它确实显示MsgBox并退出应用程序,问题是它并没有真正退出应用程序,因为执行之后的代码每次。

Here is the code I'm talking about:

这是我正在谈论的代码:

    If Not File.Exists("op.exe") Then
        MsgBox("op.exe not found!", MsgBoxStyle.Critical, "Error")
        Application.Exit()
    End If
    IO.Directory.CreateDirectory('files')

MsgBox appears, and application does exit, but it's creating the directory filesafterwards (IO.Directory.CreateDirectory('files')) . I dont want that, and I would like to completely close the application after showing the MsgBox.

MsgBox 出现,应用程序确实退出,但它files随后创建目录( IO.Directory.CreateDirectory('files')) 。我不想那样,我想在显示 MsgBox 后完全关闭应用程序。

How can I do that?

我怎样才能做到这一点?

回答by prprcupofcoffee

Try Environment.Exit(0)instead. Application.Exitcauses a message loop to exit, but that happens by the message loop reading a "quit" message from its queue. Environment.Exitcauses the process itself to exit.

试试吧Environment.Exit(0)Application.Exit导致消息循环退出,但这是通过消息循环从其队列中读取“退出”消息而发生的。Environment.Exit导致进程本身退出。