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

                }
4

1 回答 1

1

您的进程正在占用您的主线程,因此在进程运行时您将无法在该线程上执行任何操作。创建一个使用计时器偶尔输出到控制台的后台工作人员。

于 2013-07-11T18:19:12.347 回答