Вопрос

I have a console application that starts a process (which takes around 2 - 3 hours to complete)

Is there a way I can display a message saying "Process is running..." After every few minutes, below is the code :

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

                }
Это было полезно?

Решение

your process is tying up your main thread, so you won't be able to do anything on that thread while the process is running. Create a Background Worker that uses a timer to occasionally output to the Console.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top