【问题标题】:Wait for batch file to close before continuing - VB.net在继续之前等待批处理文件关闭 - VB.net
【发布时间】:2012-02-01 12:30:23
【问题描述】:

我正在尝试通过 VB 运行批处理文件,我需要等待它完成/退出才能继续。我相信我遇到的问题是,当执行批处理文件时,它会打开 cmd.exe 而不是批处理文件。

这就是我用 VB 执行的操作

        My.Computer.FileSystem.DeleteFile(My.Application.Info.DirectoryPath & "\PingCheck\machines.txt")
    FileCopy(My.Application.Info.DirectoryPath & "\machines.txt", My.Application.Info.DirectoryPath & "\PingCheck\machines.txt")

    Dim psi As New ProcessStartInfo(My.Application.Info.DirectoryPath & "\PingCheck\go.bat")
    psi.RedirectStandardError = True
    psi.RedirectStandardOutput = True
    psi.CreateNoWindow = False
    psi.WindowStyle = ProcessWindowStyle.Hidden
    psi.UseShellExecute = False

    Dim process As Process = process.Start(psi)
    process.WaitForExit()

    ProgressBar1.Value = ProgressBar1.Value + 2
    FileCopy(My.Application.Info.DirectoryPath & "\PingCheck\machines.txt", My.Application.Info.DirectoryPath & "\machines.txt")
    'My.Computer.FileSystem.DeleteFile(My.Application.Info.DirectoryPath & "\ping.bat")
    MsgBox("Ping Check Complete")

我遇到的问题是它只会在完成之前删除 ping.bat。
我如何从我调用的批处理文件中监视进程。然后一旦退出,继续执行脚本?

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    RHicke 在这里展示了如何在 VB.NET 中运行批处理的一个很好的示例,Run batch file in vb.net?

    要展开,你应该使用函数WaitForExit()来等待进程完成。

    Dim psi As New ProcessStartInfo("Path TO Batch File")
    psi.RedirectStandardError = True
    psi.RedirectStandardOutput = True
    psi.CreateNoWindow = False
    psi.WindowStyle = ProcessWindowStyle.Hidden
    psi.UseShellExecute = False
    
    Dim process As Process = Process.Start(psi)
    
    process.WaitForExit()
    

    【讨论】:

    • 谢谢,但我不相信这是有效的。我已经调整了我的脚本以匹配这个,但是它仍然显示 MessageBox 在它似乎已经完成之前。查看第一篇文章
    • 我告诉它运行 \PingCheck\go.bat 并在它完成之前显示消息框。 go.bat 实际运行并设置了一个 ping 测试循环,因此在这些测试完成之前它不会关闭
    • 我刚刚对一个批处理文件进行了快速测试,该批处理文件只是在一个错误的地址上运行一个 ping(所以它需要大约 15-20 秒),它一直等到 ping 完成后才向我显示消息盒子。您可以编辑您的问题以包含批处理代码(如果它不是很长的话)?
    • 批处理文件复杂得离谱(不是我写的)我也想看看能不能用VB做一个ping测试:stackoverflow.com/questions/9096539/…
    【解决方案2】:

    您可以使用System.Diagnostics.Process 类来启动批处理文件。流程参考将使您能够访问属性 HasExited(以及更多有趣的信息)。 HasExited 属性指示进程是否已完成。

    var process = System.Diagnostics.Process.Start(new ProcessStartInfo
                                    {
                                        FileName = "batch file path",
                                        RedirectStandardError = true,
                                        RedirectStandardOutput = true,
                                        UseShellExecute = false,
                                        Arguments = "parameters if applicable",
                                        CreateNoWindow = true
                                    });
    
    while(!process.HasExited)
    {
        // obviously do some clever here to wait 
    }    
    

    代码在 C# 中,但原理应该在 VB.NET 中工作

    【讨论】:

      【解决方案3】:

      我以前做过类似的事情。此代码使用 System.Diagnostics.Process(由 rivethead_ 提及)从 VB.Net 中调用 RoboCopy。

      Dim proc As System.Diagnostics.Process = New System.Diagnostics.Process()
      
      proc.EnableRaisingEvents = False
      proc.StartInfo.FileName = "d:\robocopy\robocopy"
      proc.StartInfo.Arguments = strSrcDir & " " & strDestDir & " " & strFile & " " & My.Settings.robocopyFlags
      proc.Start()
      proc.WaitForExit()
      

      否则,ping.bat 在做什么?它只是在执行“ping”命令吗?如果是这样,也许您可​​以使用 System.Diagnostics.Process 调用它(而不是调用 .bat 文件来执行此操作)。这可能会让您更好地控制自己的流程。

      【讨论】:

      • ping.bat 实际上会自行循环,所以我相信可能发生的情况是原始 ping.bat 在打开另一个副本后关闭。由于我没有编写批处理文件,而且它非常复杂,我无法分辨:(
      猜你喜欢
      • 1970-01-01
      • 2011-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多