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
How to completely stop running the code and exit the application?
提问by Scott
In my code, I check if the executable file op.exe
is in the application run path directory, in case if not, it does show MsgBox
and 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 files
afterwards (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.Exit
causes a message loop to exit, but that happens by the message loop reading a "quit" message from its queue. Environment.Exit
causes the process itself to exit.
试试吧Environment.Exit(0)
。Application.Exit
导致消息循环退出,但这是通过消息循环从其队列中读取“退出”消息而发生的。Environment.Exit
导致进程本身退出。