使用 Sub Main 或表单启动对象启动 VB.NET GUI 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/311346/
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
Start VB.NET GUI app using Sub Main or form startup object?
提问by ggf31416
Is there any reason to start a GUI program (application for Windows) written in VB.NET in the Sub Main of a module rather than directly in a form?
是否有任何理由在模块的 Sub Main 中而不是直接在表单中启动用 VB.NET 编写的 GUI 程序(Windows 应用程序)?
EDIT: The program won't take any command line parameters and it will be executed as a GUI program always.
编辑:该程序不会接受任何命令行参数,并且始终作为 GUI 程序执行。
回答by HTTP 410
The primary reason for using Main() in VB .NET 1.x was for adding code that needed to run before any forms were loaded. For example, you might want to detect whether an instance of your Windows Forms app was already loaded. Or you might want to intercept any unhandled exception for the AppDomain:
在 VB .NET 1.x 中使用 Main() 的主要原因是添加需要在加载任何表单之前运行的代码。例如,您可能想要检测是否已加载 Windows 窗体应用程序的实例。或者您可能想要拦截 AppDomain 的任何未处理的异常:
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf MyExceptionFilter
But the next version of VB and Visual Studio 2005 introduced a new Application modelthat made Main() unnecessary in most scenarios. You can now intercept the My.Application.Startup eventto add code that needs to run before any forms are loaded.
但是下一版本的 VB 和 Visual Studio 2005 引入了一个新的应用程序模型,这使得 Main() 在大多数情况下是不必要的。您现在可以拦截My.Application.Startup 事件以添加需要在加载任何表单之前运行的代码。
Note that the code for the Startupevent handler is stored in the ApplicationEvents.vb file, which is hidden by default.
请注意,启动事件处理程序的代码存储在 ApplicationEvents.vb 文件中,默认情况下该文件是隐藏的。
回答by JeffK
You can do it either way, but you should really only keep code in the form that is directly related to the operations and user interface elements on that form. Application startup code isn't related to UI, normally concerned with splash screens, checking network connectivity, verifying a single instance only, setting up user configuration settings, and so on.
您可以采用任何一种方式,但您实际上应该只将代码保存在与该表单上的操作和用户界面元素直接相关的表单中。应用程序启动代码与 UI 无关,通常与启动画面、检查网络连接、仅验证单个实例、设置用户配置设置等有关。
After the above items (or the appropriate initialization code for your app) are complete, Sub Main can create an instance of the main form, then show it so the user can begin interacting with your application.
在上述项目(或应用程序的适当初始化代码)完成后,Sub Main 可以创建主窗体的实例,然后显示它以便用户可以开始与您的应用程序交互。
This separates startup code from your form code. Later, when you're maintaining the application, you'll be glad you separated the two.
这将启动代码与表单代码分开。稍后,当您维护应用程序时,您会很高兴将两者分开。
回答by bugmagnet
Yes, and I have done it a few times.
是的,我已经做过几次了。
One reason is, that if your app is COM EXE (speaking now from a VB6 point of view) then you want to be able to detect in what context the EXE is being called (being launched or being spoken to by some other app).
一个原因是,如果您的应用程序是 COM EXE(现在从 VB6 的角度来看),那么您希望能够检测正在调用 EXE(正在启动或被其他应用程序与之对话)的上下文。
For example:
例如:
Sub Main()
If App.StartMode = vbSModeAutomation Then
...
Else
...
End If
End Sub
Another is if you want your app to be able to handle any command line parameters.
另一个是如果您希望您的应用程序能够处理任何命令行参数。
For example:
例如:
Sub Main()
If App.PrevInstance Then End
If InStr(Command, "/s") > 0 Then
Form1.Show
ElseIf InStr(Command, "/p") > 0 Then
LoadPicture ("c:\windows\Zapotec.bmp")
End If
End Sub
(from one of my attempts to make a screen saver)
(来自我尝试制作屏幕保护程序的一次)
回答by Arvo
No, if you always want to show that form.
Yes, if you sometimes want to use your app without GUI, just using command line.
不,如果您总是想显示该表格。
是的,如果您有时想在没有 GUI 的情况下使用您的应用程序,只需使用命令行。