Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8060159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:54:07+00:00 2026-06-05T09:54:07+00:00

I have a ChildProcessMonitor class that launches my process, reports data received and restarts

  • 0

I have a ChildProcessMonitor class that launches my process, reports data received and restarts the process when it exits. My problem is that once the process exits and Start is called again, output is no longer reported.

using System;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace WcfClient
{
    /// <summary>
    /// Can be used to launch and monitor (restart on crash) the child process.
    /// </summary>
    public class ChildProcessMonitor
    {
        private Process _process;

        /// <summary>
        /// Starts and monitors the child process.
        /// </summary>
        /// <param name="fullProcessPath">The full executable process path.</param>
        public void StartAndMonitor(string fullProcessPath)
        {
            StartAndMonitor(fullProcessPath, null);
        }

        /// <summary>
        /// Starts and monitors the child process.
        /// </summary>
        /// <param name="fullProcessPath">The full executable process path.</param>
        /// <param name="arguments">The process arguments.</param>
        public void StartAndMonitor(string fullProcessPath, string arguments)
        {
            ProcessStartInfo processStartInfo = new ProcessStartInfo
            {
                CreateNoWindow = true,
                FileName = fullProcessPath,
                WorkingDirectory = Path.GetDirectoryName(fullProcessPath) ?? string.Empty,
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true
            };

            processStartInfo.Arguments = arguments;             

            _process = new Process { StartInfo = processStartInfo, EnableRaisingEvents = true };
            _process.OutputDataReceived += OnOutputDataReceived;
            _process.ErrorDataReceived += OnErrorDataReceived;
            _process.Start();
            _process.BeginOutputReadLine();
            _process.BeginErrorReadLine();
            _process.Exited += OnProcessExited;
        }

        /// <summary>
        /// Called when process exits.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void OnProcessExited(object sender, EventArgs e)
        {
            if (_process != null)
            {
                Thread.Sleep(2000);                 
                _process.Start();                       
            }
        }

        /// <summary>
        /// The ErrorDataReceived event indicates that the associated process has written to its redirected StandardError stream.
        /// </summary>
        public DataReceivedEventHandler ErrorDataReceived;

        /// <summary>
        /// Called when error data received.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Diagnostics.DataReceivedEventArgs"/> instance containing the event data.</param>
        private void OnErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
            Trace.WriteLine("Error data.");
            if (ErrorDataReceived != null)
            {
                ErrorDataReceived(sender, e);
            }
        }

        /// <summary>
        /// The OutputDataReceived event indicates that the associated Process has written to its redirected StandardOutput stream.
        /// </summary>
        public DataReceivedEventHandler OutputDataReceived;

        /// <summary>
        /// Called when output data received.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Diagnostics.DataReceivedEventArgs"/> instance containing the event data.</param>
        private void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            Trace.WriteLine("Output data.");
            if (OutputDataReceived != null)
            {
                OutputDataReceived(sender, e);
            }
        }

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:54:08+00:00Added an answer on June 5, 2026 at 9:54 am

    Try use:

    private void OnProcessExited(object sender, EventArgs e)
    {
      if (_process != null)
      {
        Thread.Sleep(2000);
        _process.CancelOutputRead();
        _process.CancelErrorRead();
        _process.Start();
        _process.BeginOutputReadLine();
        _process.BeginErrorReadLine();
    
      }
    }
    

    ps

    Short description: OutputRead & ErrorRead closed with process restarting.

    Long description with code from reflection:

    public void BeginOutputRead()
    {
      [..]
      if (this.output == null)
      {
        [..]                    
        this.output = new AsyncStreamReader(this, baseStream, new UserCallBack(this.OutputReadNotifyUser), this.standardOutput.CurrentEncoding);
      }
    }
    
    public void Start()
    {   
        this.Close();
        [..]
    }
    
    public void Close()
    {   
        [..]
        this.output = null;
        this.error = null;
        [..]
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have data that has this kind of structure. Will be in ascending order by
Have data that has this kind of structure: $input = [ { animal: 'cat',
Have a look at one of my websites: moskah.com The problem is that it
Have a problem that seems easy on paper but i'm having a big problem
Have the following problem example . The problem is that because of the border
Have one Doubt In MVC Architecture we can able to pass data from Controller
Have a network location that shows paths in the 8.3 short format. I need
Have a bunch of WCF REST services hosted on Azure that access a SQL
Have a simple contact us XPage created. Have server side validation in place that
Have written all the code in a silverlight class library (dll) and linked this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.