2412

Reusing Process object and IO redirects in C#

Question:

I'm using the data ready events of the Process class to get information from the standard output and standard error of a running process.

It works great on the first run, but after calling Stop() then Start() to force a restart of the application, I no longer recieve data. I've tried CancelErrorRead() but no luck there.

I'm considering just re-instantiating the object every time I need to re-run the app, but it seems silly to need to do that.

Any advice on how to re-use a Process object to restart a stopped process?

Relevant code chunks:

Constructor:

ProcessStartInfo objStartInfo = new ProcessStartInfo(); objStartInfo.CreateNoWindow = true; objStartInfo.RedirectStandardInput = true; objStartInfo.RedirectStandardOutput = true; objStartInfo.RedirectStandardError = true; objStartInfo.UseShellExecute = false; objClient = new Process(); objClient.StartInfo = objStartInfo; objClient.EnableRaisingEvents = true; objClient.OutputDataReceived += new DataReceivedEventHandler(read); objClient.ErrorDataReceived += new DataReceivedEventHandler(error);

Start:

objClient.StartInfo.FileName = strAppPath; objClient.StartInfo.Arguments = strArgs; start(); objClient.BeginErrorReadLine(); objClient.BeginOutputReadLine();

Stop:

objClient.Close(); objClient.CancelErrorRead(); objClient.CancelOutputRead();

Answer1:

Your Process object is not associated with a process until you call Start() (or use one of the static methods off Process). A stopped/closed process is functionally the same as no process at all. Given that, it's hard to believe there's any overhead to creating a new Process object, when compared to the (relatively enormous) cost of creating processes on Windows. Just create new Process objects as needed.

Answer2:

According to msdn you should call BeginOutputReadLine and BeginErrorReadLine to enable asynchronous reads from StandardOutput or StandardError using events.

Have a look at the remarks section on <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline.aspx" rel="nofollow">BeginOutputReadLine</a>

Recommend

  • (Xcode, Swift) Is it possible to load multiple WKWebViews simultaneously if they are on different vi
  • What is the absolutely cheapest way to select a child node in Nokogiri?
  • Deserializing Json String containing interfaces not working in Java using Gson 2.3.1
  • PHP - access function from a class
  • Value assigned is not used in any execution path - C# [duplicate]
  • What int should I pass into an ArrayAdapter constructor if I am not using an xml resource?
  • synchronized method in stateless session bean not working as expected in glassfish
  • Extending an Array properly, keeping the instance of subclass
  • Instance initialization block and subclasses
  • Instantiating generic class and implementing generic interface
  • Parse Javascript Array
  • Saving application state AIR
  • Behaviour of System.Timer when Interval property changed
  • Prolog binary search tree test - unwanted parents' parent node comparison
  • IntelliJ - debug mode - search text in program memory
  • Maple: How to insert a command to force my code to choose random integer values to find my values?
  • C# Application Relative Paths
  • Enterprise Architect - Cancel Connectors Bridges
  • Update SQL MS Access 2010
  • f:param to composite components
  • Is it possible to run an application built on sql server 2008 to run with 2005
  • Implementing Oracle.Web.dll, Oracle.DataAccess.dll 64-bit failed on Windows Server 2008 with IIS 7.5
  • Run EF6 Query in separate Thread on WinForm Button Click Event
  • AVCaptureSession VS UIImagePickerController camera preview
  • Best practice for switching iPhone views?
  • Accessing the variables from a PHP Anonymous Function
  • Generic classes with Collection getter of other types
  • Error in making a socket connection
  • Display images in Django
  • Word Open XML Mail Merge
  • req.body is undefined - nodejs
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Symfony2: How to get request parameter
  • Akka Routing: Reply's send to router ends up as dead letters
  • Is there a mandatory requirement to switch app.yaml?
  • How do I configure my settings file to work with unit tests?
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Is it possible to post an object from jquery to bottle.py?