0
所以,我有這個代碼來調用一個批處理文件。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,這個過程工作正常。 這是怎麼回事?
這很混亂。也許你想改變If(myProcess.HasExited)然後如果不是myProcess.HasExited那麼。 – dbasnett
@dbasnett我想檢查批處理文件是否正常運行。所以我使用If(myProcess.HasExited)。該過程應在3分鐘內完成,否則應退出並顯示錯誤。 – user2500290
你只是向後測試。它應該是**不是** myProcess.HasExited。而是使用WaitForExit()的返回值。 –