ProcessStartInfo .WaitForExit()不能正常工作
问题描述:
所以,我有这个代码来调用一个批处理文件。ProcessStartInfo .WaitForExit()不能正常工作
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,这个过程工作正常。 这是怎么回事?
答
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
+0
是的,我现在明白我的愚蠢。 Thnx @Ondrej Svejdar – user2500290
这很混乱。也许你想改变If(myProcess.HasExited)然后如果不是myProcess.HasExited那么。 – dbasnett
@dbasnett我想检查批处理文件是否正常运行。所以我使用If(myProcess.HasExited)。该过程应在3分钟内完成,否则应退出并显示错误。 – user2500290
你只是向后测试。它应该是**不是** myProcess.HasExited。而是使用WaitForExit()的返回值。 –