2013-04-21 83 views
0

我想啓動一個cmd文件並立即得到輸出。重定向輸出的進程

看看我的代碼。導入process.WaitForExit()不等;爲什麼不? copyf.cmd運行良好,如果我不以隱藏模式啓動它,因爲顯示的dosbox運行到cmd的末尾。 在隱藏模式下,cmd被關閉,因爲process.WaitForExit()不會完成,直到cmd完成。

public void doSomeThing( Queue<string> output, // queue for output log 
         Queue<string> error // queue for error log 
         ) 
{ 
      String com = "some params"; 

      System.Diagnostics.Process process = new System.Diagnostics.Process(); 
      System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); 
      startInfo.FileName = Properties.Settings.Default.pathTo + @"\Make\copyf.cmd"; 
      startInfo.Arguments = com; 
      startInfo.RedirectStandardOutput = true; 
      startInfo.RedirectStandardError = true; 
      startInfo.UseShellExecute = false; 
      startInfo.CreateNoWindow = true; 
      startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
      process.StartInfo = startInfo; 

      Thread thread = new Thread(new ThreadStart(() => 
      { 
       String er; 
       String outp; 

       while (true) 
       { 
        outp = process.StandardOutput.ReadLine(); 

        if(outp != null) 
         output.Enqueue("Output :" + outp + "\n"); 

        er = process.StandardError.ReadLine(); 
        if (er != null) 
         error.Enqueue("Error :" + er + "\n"); 
       } 
      })); 

      process.Start(); 
      thread.Start(); 
      process.WaitForExit(); 
} 
+0

爲什麼你認爲'WaitForExit'不等待進程退出? – Ryan 2013-04-21 21:35:51

+0

我發現'.WaitForExit()'不能工作交叉線程,相反我相信我用'while(!proc.HasExited)'(想法比評論更有意思) – Sayse 2013-04-21 21:37:08

+0

好吧,它不是交叉線程。該線程只讀取輸出/錯誤。 「進程」runnig在process.WaitForExit(9被調用。 – tux007 2013-04-21 21:39:00

回答

0

您應該使用C開關啓動實際控制檯命令處理器「cmd.exe」,而不是調用.CMD文件。

/C開關將保持cmd.exe過程一直持續到您的命令文件執行完畢。 WaitForExit()將一直等到CMD.exe完成執行。我曾多次使用它。對於WaitForExit的正確操作,必須在StandardError.ReadToEnd()後調用它。作爲described here in msdn修改後的版本如下:

string command = string.Format("{0}\\Make\\copyf.cmd {1}", Properties.Settings.Default.pathTo , com); 
    int waitTime = 60000; //1 min as example here 
    using (var process = new Process() 
    { 
     StartInfo = new ProcessStartInfo() 
       { 
        UseShellExecute = false, 
        RedirectStandardOutput = true, 
        CreateNoWindow = true, 
        RedirectStandardInput = true, 
        RedirectStandardError = true, 
        FileName = "cmd.exe", 
        Arguments = string.Format("/C \"{0}\"", command) 
       } 
    }) 
    { 
     try 
     { 
      if (!process.Start()) 
      { 
       //Log some error "Failed to run command"; 
      } 
     } 
     catch (Exception ex) 
     { 
      //Log some error "Failed to start process command"; 
     } 

     process.BeginOutputReadLine(); 
     string error = process.StandardError.ReadToEnd(); 
     process.WaitForExit(waitTime); 
     if (!process.HasExited) 
     { 
      //Log something "Command: did not finish in time, terminating process" 
      try 
      { 
       process.Kill(); 
      } 
      catch { } 
     } 
    } 
+0

不,它不起作用。 – tux007 2013-04-22 00:47:40

+0

你是說,waitforexit不起作用?或cmd.exe不起作用? – loopedcode 2013-04-22 00:49:55

+0

發佈修改後的版本,請注意對StandardError.ReadToEnd()的調用,忘記將它放入我的第一個響應中。 – loopedcode 2013-04-22 01:07:18

相關問題