ProcessStartInfo .WaitForExit()无法正常工作

时间:2013-06-21 13:02:59

标签: .net vb.net visual-studio-2010

所以,我有这个代码来调用批处理文件。

        If System.IO.File.Exists(FSourceFile) Then
            Dim psi As New ProcessStartInfo(batchFileLoc + batchFileName)
            psi.RedirectStandardOutput = True
            psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
            psi.UseShellExecute = False
            Dim myProcess As Process = Process.Start(psi)
            Dim output As String = myProcess.StandardOutput.ReadToEnd()
            myProcess.WaitForExit(180000)
            If (myProcess.HasExited) Then
                Throw New Exception("FTP failed due to time-out. Please check the connectivity to FTP server.")
            End If
            FTPFile = "Success"
        End If

如果批处理文件执行未在3分钟内完成,我希望“myProcess”退出。但即使批处理文件执行在不到2秒的时间内完成,      myProcess.HasExited
返回True。 如果我把2000而不是180000,这个过程运行正常。 这是怎么回事?

1 个答案:

答案 0 :(得分:1)

myProcess.HasExited只是告诉您进程是否已退出。如果您对由于超时而退出流程感兴趣,请使用

If Not myProcess.WaitForExit(180000) Then
    Throw New Exception("FTP failed due to time-out. Please check the connectivity to FTP server.")
End If