共用方式為


HOW TO:啟用 Window Form 應用程式的批次模式

更新:2007 年 11 月

這個範例會使用 My.Application.Startup 事件,檢查應用程式是否以字串 /batch 做為引數而啟動。

若要啟用 Window Form 應用程式的批次模式

  1. 在 [方案總管] 中選取專案。在 [專案] 功能表上,按一下 [屬性]。

  2. 在 [應用程式] 索引標籤上,按一下 [檢視應用程式事件] 以開啟 [程式碼編輯器]。

  3. 建立方法,用於處理 My.Application.Startup 事件。如需詳細資訊,請參閱 HOW TO:處理應用程式事件 (Visual Basic)

    Private Sub MyApplication_Startup( _
        ByVal sender As Object, _
        ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs _
    ) Handles Me.Startup
    
    End Sub
    
  4. 逐一查看應用程式的命令列引數,如果其中一個引數為 /batch,則將 e 物件的 Cancel 屬性設定為 True。

    當 Cancel 屬性設為 True 時,啟動表單便不會啟動。

    For Each s As String In My.Application.CommandLineArgs
        If s.ToLower = "/batch" Then
            ' Stop the start form from loading.
            e.Cancel = True
        End If
    Next
    
  5. 如果 e 物件的 Cancel 屬性已設定為 True,則呼叫無視窗 (Windowless) 作業的主常式。

    If e.Cancel Then
        ' Call the main routine for windowless operation.
        Dim c As New BatchApplication
        c.Main()
    End If
    

範例

Private Sub MyApplication_Startup( _
    ByVal sender As Object, _
    ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs _
) Handles Me.Startup
    For Each s As String In My.Application.CommandLineArgs
        If s.ToLower = "/batch" Then
            ' Stop the start form from loading.
            e.Cancel = True
        End If
    Next
    If e.Cancel Then
        ' Call the main routine for windowless operation.
        Dim c As New BatchApplication
        c.Main()
    End If
End Sub
Class BatchApplication
    Sub Main()
        ' Insert code to run without a graphical user interface.
    End Sub
End Class

請參閱

工作

HOW TO:存取命令列引數 (Visual Basic)

概念

Visual Basic 應用程式模型概觀

參考

My.Application 物件

My.Application.CommandLineArgs 屬性