杀死在后端运行的批处理

时间:2013-10-22 06:01:38

标签: windows vb.net batch-file

最近开发使用VB,就像从一个洞跳到另一个......

这是我的问题。我使用Visual Basic,单击开始按钮 - >在后端运行批处理文件,用户无法看到它,并且还有一个按钮'Abort',当它被点击时,该进程(后端运行的批处理文件)应该被杀死。

然而,它没有。

Dim pathtobatfile As String
    pathtobatfile = mainPath & "TEMP\extract.bat"
    Dim psi As New ProcessStartInfo(pathtobatfile)
    psi.RedirectStandardError = True
    psi.RedirectStandardOutput = True
    psi.CreateNoWindow = True
    psi.WindowStyle = ProcessWindowStyle.hidden

    psi.UseShellExecute = False
    process= process.Start(psi)

以上设置启动流程

    process.CloseMainWindow() 
    process.Kill()
    process.Close()

一切都行不通,但如果设置'psi.CreateNoWindow = false' 并使用process.CloseMainWindow()关闭批处理文件窗口,它将停止。 但我不希望CMD窗口弹出给用户......

1 个答案:

答案 0 :(得分:2)

你设置了不必要的东西来隐藏窗口,试试这个:

在程序之外声明进程:

Private p As New Process With {.StartInfo = New ProcessStartInfo With { _
           .FileName = string.empty, _
           .RedirectStandardError = True, _
           .RedirectStandardOutput = True, _
           .CreateNoWindow = True, _
           .UseShellExecute = False _
}}

则...

Private Sub StartProcess()
    p.Filename = mainPath & "TEMP\extract.bat"
    p.Start()
End Sub

Private Sub KillProcess()
    If Not p.HasExited() Then
        p.Kill()
    End If
End Sub