문제

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