Visual Basic의 Main 프로시저

모든 Visual Basic 애플리케이션에는 Main(이)라는 프로시저가 포함되어야 합니다. 이 프로시저는 애플리케이션의 시작점이자 전반적인 제어 역할을 합니다. .NET Framework는 애플리케이션을 로드하고 제어권을 전달할 준비가 되면 Main 프로시저를 호출합니다. Windows Forms 애플리케이션을 만들지 않는 한 자체 실행되는 애플리케이션에 대한 Main 절차를 작성해야 합니다.

Main에는 우선 실행되는 코드가 포함되어 있습니다. Main에서 프로그램이 시작될 때 우선 로드될 양식을 확인하거나, 애플리케이션의 복사본이 시스템에서 이미 실행 중인지 확인하거나, 애플리케이션에 대한 변수 집합을 설정하거나, 애플리케이션에 필요한 데이터베이스를 열 수 있습니다.

주 프로시저에 대한 요구 사항

자체 실행되는 파일(일반적으로 확장명 .exe 포함)에는 Main 프로시저가 포함되어야 합니다. 라이브러리(예: 확장명 .dll 포함)는 자체 실행되지 않으며 Main 프로시저가 필요하지 않습니다. 만들 수 있는 다양한 유형의 프로젝트에 대한 요구 사항은 다음과 같습니다.

  • 콘솔 애플리케이션은 자체 실행되며 하나 Main 이상의 프로시저를 제공해야 합니다.

  • Windows Forms 애플리케이션은 자체 실행됩니다. 그러나 Visual Basic 컴파일러는 이러한 애플리케이션에서 Main 프로시저를 자동으로 생성하므로 프로시저를 작성할 필요가 없습니다.

  • 클래스 라이브러리에는 Main 프로시저가 필요하지 않습니다. 여기에는 Windows 컨트롤 라이브러리 및 웹 컨트롤 라이브러리가 포함됩니다. 웹 애플리케이션은 클래스 라이브러리로 배포됩니다.

주 프로시저 선언

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 값을 검사하여 이 코드를 테스트할 수 있습니다. 종료 코드를 반환하려면 Sub 프로시저 대신 Function 프로시저로 Main(을)를 선언 해야 합니다.

    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
    

참고 항목