使用 Sub Main 或表单启动对象启动 VB.NET GUI 应用程序? [英] Start VB.NET GUI app using Sub Main or form startup object?

查看:30
本文介绍了使用 Sub Main 或表单启动对象启动 VB.NET GUI 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何理由在模块的 Sub Main 中而不是直接在表单中启动用 VB.NET 编写的 GUI 程序(Windows 应用程序)?

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?

该程序不会接受任何命令行参数,它将始终作为 GUI 程序执行.

The program won't take any command line parameters and it will be executed as a GUI program always.

推荐答案

在 VB .NET 1.x 中使用 Main() 的主要原因是添加需要在加载任何表单之前运行的代码.例如,您可能想要检测是否已加载 Windows 窗体应用程序的实例.或者您可能想要拦截 AppDomain 的任何未处理的异常:

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:

AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf MyExceptionFilter

但是下一版本的 VB 和 Visual Studio 2005 引入了一个新的应用程序模型 在大多数情况下不需要 Main() .您现在可以拦截 My.Application.Startup 事件 添加需要在加载任何表单之前运行的代码.

But the next version of VB and Visual Studio 2005 introduced a new Application model that made Main() unnecessary in most scenarios. You can now intercept the My.Application.Startup event to add code that needs to run before any forms are loaded.

请注意,Startup 事件处理程序的代码存储在 ApplicationEvents.vb 文件中,默认情况下该文件是隐藏的.

Note that the code for the Startup event handler is stored in the ApplicationEvents.vb file, which is hidden by default.

这篇关于使用 Sub Main 或表单启动对象启动 VB.NET GUI 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆