2013-06-21 30 views
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,這個過程工作正常。 這是怎麼回事?

+2

這很混亂。也許你想改變If(myProcess.HasExited)然後如果不是myProcess.HasExited那麼。 – dbasnett

+0

@dbasnett我想檢查批處理文件是否正常運行。所以我使用If(myProcess.HasExited)。該過程應在3分鐘內完成,否則應退出並顯示錯誤。 – user2500290

+0

你只是向後測試。它應該是**不是** myProcess.HasExited。而是使用WaitForExit()的返回值。 –

回答

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 
+0

是的,我現在明白我的愚蠢。 Thnx @Ondrej Svejdar – user2500290