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...")
}