2013-07-11 53 views
0

我有一個控制檯應用程序,開始進行處理(這大約需要2 - 3小時以完成)顯示信息在等待過程結束

有一種辦法可以顯示一條消息「進程運行......」每隔幾分鐘後,下面是代碼:

  using (process) 
      { 
       var output = new StringBuilder(); 
       var error = new StringBuilder(); 

       using (var outputWaitHandle = new AutoResetEvent(false)) 
       using (var errorWaitHandle = new AutoResetEvent(false)) 
       { 
        process.OutputDataReceived += (sender, e) => 
        { 
         if (e.Data == null) 
         { 
          outputWaitHandle.Set(); 
         } 
         else 
         { 
          output.AppendLine(e.Data); 
         } 
        }; 
        process.ErrorDataReceived += (sender, e) => 
        { 
         if (e.Data == null) 
         { 
          errorWaitHandle.Set(); 
         } 
         else 
         { 
          error.AppendLine(e.Data); 
         } 
        }; 

        process.Start(); 
        Console.WriteLine("Process is running..."); // THIS ONLY DISPLAYS ONCE 

        process.BeginOutputReadLine(); 
        process.BeginErrorReadLine(); 
        process.WaitForExit(); 
        Console.WriteLine("Process has shutdown...")  

       } 

回答

1

你的進程佔用你的主線程,所以你將無法在過程運行在該線程上做任何事情。創建一個使用定時器偶爾輸出到控制檯的Background Worker