How to: Enable a Batch Mode for Window Forms Applications

This example uses the My.Application.Startup event to check if the application started with the string /batch as an argument.

To enable a batch mode for a Window Forms Application

  1. Have a project selected in Solution Explorer. On the Project menu, click Properties.

  2. On the Application tab, click View Application Events to open the Code Editor.

  3. Create the method that handles the My.Application.Startup Event. For more information, see How to: Handle Application Events (Visual Basic).

    Private Sub MyApplication_Startup( _
        ByVal sender As Object, _
        ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs _
    ) Handles Me.Startup
    
    End Sub
    
  4. Iterate through the application's command-line arguments, and set the Cancel property of the e object to True if one of the arguments is /batch.

    When the Cancel property is set to True, the startup form does not start.

    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. If the Cancel property of the e object is set to True, call the main routine for windowless operation.

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

Example

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

See Also

Tasks

How to: Access Command-Line Arguments (Visual Basic)

Concepts

Overview of the Visual Basic Application Model

Reference

My.Application Object

My.Application.CommandLineArgs Property