Visual Basic 中的主过程

每个 Visual Basic 应用程序都必须包含一个名为 Main 的过程。 此过程充当应用程序的起点和整体控制。 .NET Framework 在加载应用程序并准备好向其传递控制权时调用 Main 过程。 除非要创建 Windows 窗体应用程序,否则必须为自行运行的应用程序编写 Main 过程。

Main 包含首先运行的代码。 在中 Main,可以确定程序启动时首先加载哪个窗体,了解应用程序的副本是否已在系统上运行,为应用程序建立一组变量,或打开应用程序所需的数据库。

主要过程的要求

自行运行的文件(通常具有扩展名 .exe)必须包含一个 Main 过程。 库(例如,扩展 .dll)不自行运行,不需要过程 Main 。 可以创建的不同类型的项目的要求如下:

  • 控制台应用程序自行运行,并且必须至少提供一个 Main 函数。

  • Windows 窗体应用程序自行运行。 但是,Visual Basic 编译器会在此类应用程序中自动生成过程 Main ,无需编写过程。

  • 类库不需要 Main 过程。 其中包括 Windows 控件库和 Web 控件库。 Web 应用程序部署为类库。

声明主要过程

有四种方法可以声明该Main过程。 它可以接受参数或不接受,并且可以返回值或不返回值。

注释

如果在类中声明 Main ,则必须使用 Shared 关键字。 在模块中, Main 不需要是 Shared

  • 最简单的方法是定义一个不接受参数也不返回值的Sub程序。

    Module mainModule
        Sub Main()
            MsgBox("The Main procedure is starting the application.")
            ' Insert call to appropriate starting place in your code.
            MsgBox("The application is terminating.")
        End Sub
    End Module
    
  • Main 还可以返回一个 Integer 值,作系统将该值用作程序的退出代码。 其他程序可以通过检查 Windows ERRORLEVEL 值来测试此代码。 若要返回退出代码,必须声明 Main 为过程 Function 而不是 Sub 过程。

    Module mainModule
        Function Main() As Integer
            MsgBox("The Main procedure is starting the application.")
            Dim returnValue As Integer = 0
            ' Insert call to appropriate starting place in your code.
            ' On return, assign appropriate value to returnValue.
            ' 0 usually means successful completion.
            MsgBox("The application is terminating with error level " &
                 CStr(returnValue) & ".")
            Return returnValue
        End Function
    End Module
    
  • Main 还可以将 String 数组作为参数。 数组中的每个字符串都包含用于调用程序的命令行参数之一。 你可以根据其值执行不同的操作。

    Module mainModule
        Function Main(ByVal cmdArgs() As String) As Integer
            MsgBox("The Main procedure is starting the application.")
            Dim returnValue As Integer = 0
            ' See if there are any arguments.
            If cmdArgs.Length > 0 Then
                For argNum As Integer = 0 To UBound(cmdArgs, 1)
                    ' Insert code to examine cmdArgs(argNum) and take
                    ' appropriate action based on its value.
                Next
            End If
            ' Insert call to appropriate starting place in your code.
            ' On return, assign appropriate value to returnValue.
            ' 0 usually means successful completion.
            MsgBox("The application is terminating with error level " &
                 CStr(returnValue) & ".")
            Return returnValue
        End Function
    End Module
    
  • 可以声明 Main 检查命令行参数,但不返回退出代码,如下所示。

    Module mainModule
        Sub Main(ByVal cmdArgs() As String)
            MsgBox("The Main procedure is starting the application.")
            Dim returnValue As Integer = 0
            ' See if there are any arguments.
            If cmdArgs.Length > 0 Then
                For argNum As Integer = 0 To UBound(cmdArgs, 1)
                    ' Insert code to examine cmdArgs(argNum) and take
                    ' appropriate action based on its value.
                Next
            End If
            ' Insert call to appropriate starting place in your code.
            MsgBox("The application is terminating.")
        End Sub
    End Module
    

另请参阅