【发布时间】:2013-07-11 18:13:11
【问题描述】:
我有一个启动进程的控制台应用程序(大约需要 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...")
}
【问题讨论】:
标签: c# console-application progress waitforexit