0

I'm sorry if the title is not matching the precise description of the issue I'm facing, I accept suggestions for edits.

I'm developing a component that, when called, shall start a Process and wait for it to complete. The Process might want to redirect std output to a custom winform (a popup) for the user to have a feedback on the Process status.

In order to test the component before implementing it in the official application, I've developed a Console app that calls the module, in the same way the official application will do accepting user input from the GUI --> not the popup, the application main window.

When tested in the Console app, everything works as I expect. When testing in the official GUI application, the Process ends prematurely, with exit code 0 (all fine). I mean, it prints something in the user winform, up to a certain point, always the same point, then exits.

I can't understand why or how to identify root causes and maybe do something about it. I'm looking for help, hereafter the module source code for process execution:

ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = myExePath;
processStartInfo.Arguments = myExeArguments;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = false;
processStartInfo.UseShellExecute = false;
processStartInfo.CreateNoWindow = true;
using (Process process = Process.Start(processStartInfo))
{
    process.EnableRaisingEvents = true;

    // accessory code. Issue is reproduced even without this section ---- //
    Thread t = new Thread(() =>
    {
        // show a custom winform for the user to read
    });
    t.Start();
    // ------------------------------------------ //

    process.OutputDataReceived += (s, a) => {
        // Do something with a.Data
    };

    process.BeginOutputReadLine();
    process.WaitForExit();
}

EDIT:

let's say that the Process myExe should print something to stdout. I read this something and it goes along these lines:

line 1
line 2
I'm doing something
line 3
start doing something else <-- when component is integrated in official application, here Process Exits without exceptions
line 4
line 5 <-- When component is tested in Console app, here is where Process Exits without exceptions.

I should read up to line 5 in both testing scenario and official scenario.

Daemon Painter
  • 3,208
  • 3
  • 29
  • 44
  • The concerning part is starting a thread to show the "custom winform" to the user. If that is the case, you should be starting the form on the UI thread, not in its own thread. The main application thread should be marked STA. The biggest issue I see is the cross-thread calls to GUI objects. – Ron Beyer Jun 05 '19 at 14:12
  • Do you get an [unhandled exception](https://stackoverflow.com/q/5762526/1997232)? – Sinatr Jun 05 '19 at 14:14
  • Adding to Ron's comment: ...that could raise exceptions that could cause the behavior described. – Cleptus Jun 05 '19 at 14:15
  • @Sinatr, no. Process exits just fine. Process doesn't exit at the same point when executed from Console app. – Daemon Painter Jun 05 '19 at 14:35
  • @ Ron Beyer, removing completely the winform does not change the issue: Process Exits with exit code 0 prematurely. – Daemon Painter Jun 05 '19 at 14:40
  • Can you try to launch it from a new simple GUI app? One potential workaround that might work for you is to run the process through cmd.exe. That is running the equivalent of "cmd /c – Mohammad Jun 05 '19 at 14:48
  • 1
    Note that `EnableRaisingEvents` is only used to raise the [Exited](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.exited) event, thus using the asynchronous version (you should use this mode) instead of the blocking `WaitForExit()`. You should also redirect `stdError` and see what happens there. See this example: [How do I get output from a command to appear in a control on a Form in real-time?](https://stackoverflow.com/a/51682585/7444103). – Jimi Jun 05 '19 at 15:54

0 Answers0