1

I've seen a ton of articles on Stack Overflow (and the rest of the Internet) for starting a command-line process in the background of a C# application, however all of the examples put a process.WaitForExit() command at the end.

My issue is that I need to keep the process alive in the background so that my WPF app can communicate with it (client/server style). My code successfully runs the process and my app can communicate with it, but I can't seem to capture any of the output.

Simplified code.

In App.xaml.cs:

private Process hostApplication;
private FileInfo hostApplicationFilename;
private void Application_Startup(object sender, StartupEventArgs e)
{
    // Start the command line process
    hostApplication = new Process();
    hostApplicationFilename = new FileInfo(@"C:\pretendpath\myEXE.exe");

    hostApplication.StartInfo.FileName = hostApplicationFilename.FullName;
    hostApplication.StartInfo.WorkingDirectory = hostApplicationFilename.DirectoryName;

    hostApplication.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    hostApplication.StartInfo.CreateNoWindow = true;

    hostApplication.StartInfo.UseShellExecute = false;
    hostApplication.StartInfo.RedirectStandardOutput = true;
    hostApplication.StartInfo.RedirectStandardError = true;
    hostApplication.StartInfo.RedirectStandardInput = true;

    hostApplication.OutputDataReceived += new DataReceivedEventHandler(HostApplicationOutputHandler);
    hostApplication.ErrorDataReceived += new DataReceivedEventHandler(HostApplicationOutputHandler);
    hostApplication.EnableRaisingEvents = true;

    hostApplication.Start();
    hostApplication.BeginOutputReadLine();
    hostApplication.BeginErrorReadLine();

    // Start the WPF's main window
    MainWindow view = new MainWindow();
    view.Show();
}

private void HostApplicationOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
    if (!String.IsNullOrEmpty(outLine.Data))
    {
        LocalHostApplicationLog = Environment.NewLine + outLine.Data;
#if (DEBUG)
        Console.WriteLine("HOST APPLICATION OUTPUT: " + outLine.Data);
#endif
    }
}

If I put a breakpoint inside the DataReceivedEventHandler, it's never hit.

Any suggestions would be greatly appreciated!

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
savetruman
  • 145
  • 7
  • You should perhaps create a new thread, start your client process in that thread, and waitforexit in that thread. – DotThoughts Mar 26 '14 at 19:06
  • I have now tried that. I put all of the above code related to `hostApplication` in its own thread and added a `hostApplication.WaitForExit()` at the end. What happens now is that the breakpoint in the DataReceivedEventHandler gets hit once I close the MainWindow. Once the MainWindow is closed, all of the process's output gets dumped at once. – savetruman Mar 26 '14 at 21:53
  • It may be that the client process is buffering the output stream - is it something you have control over? If you do, can you flush the output stream every time you write to it? – DotThoughts Mar 27 '14 at 13:17
  • If you do not have any control over the client process, another way to redirect the output/error streams is as shown in [another post on SO](http://stackoverflow.com/questions/164736/redirect-standard-output-efficiently-in-net) – DotThoughts Mar 27 '14 at 13:19
  • The method used in the post you linked to worked until I tried to also redirect input. – savetruman Apr 11 '14 at 00:22
  • I do have some control over the process, and flushing the stream works. Now I just have to convince the owner of that software to include the flushing. Thanks for the help! – savetruman Apr 11 '14 at 00:23
  • Excellent! Glad to be of help. – DotThoughts Apr 11 '14 at 19:10

0 Answers0