如何:为 Window 窗体应用程序启用批处理模式

更新:2007 年 11 月

此示例使用 My.Application.Startup 事件检查应用程序是否通过作为参数的 /batch 字符串启动。

为 Window 窗体应用程序启用批处理模式

  1. 在“解决方案资源管理器”中选择一个项目。在“项目”菜单上单击“属性”。

  2. 在“应用程序”选项卡上,单击“查看应用程序事件”以打开代码编辑器。

  3. 创建处理 My.Application.Startup 事件事件的方法。有关更多信息,请参见如何:处理应用程序事件 (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,则调用主例程以执行无窗口操作。

    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

请参见

任务

如何:访问命令行参数 (Visual Basic)

概念

Visual Basic 应用程序模型概述

参考

My.Application 对象

My.Application.CommandLineArgs 属性