1

I'm starting a process with Process.Start:

var psi = new ProcessStartInfo("yt-dlp.exe"){
    ErrorDialog = false,
    CreateNoWindow = true,
    RedirectStandardError = true,
    RedirectStandardOutput = true,
                
    ArgumentList = {
        "--prefer-insecure",
        "--no-check-certificates",
        "--dump-json",
        "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
    },
};

var process = Process.Start(psi);
process.EnableRaisingEvents = true;
process.Exited += HandleProcessExit;

HandleProcessExit is never called - the process never actually exits, judging by the fact that the process does not disappear from the Task Manager. Calling the process itself outside of C# (yt-dlp.exe --prefer-insecure --no-check-certificates --dump-json https://www.youtube.com/watch?v=dQw4w9WgXcQ) in my command terminal works fine - the process exits.

Changing RedirectStandardError and RedirectStandardOutput to false seems to fix the issue... however, I need to access these streams.

For context, the executable I'm using is yt-dlp, but I don't think that is the issue. How can I fix this?

  • Did you try adding handlers for OutputDataReceived and ErrorDataReceived, to read through them? – Alexandru Clonțea Jun 03 '22 at 11:19
  • @AlexandruClonțea Yep, OutputDataReceived isn't fired at all, no matter if `RedirectStandardError` and `RedirectStandardOutput` are both set to `false` or `true` – zbigniew flet Jun 03 '22 at 11:22
  • Maybe the process writes to stderr instead then, I'd suggest adding a handler for ErrorDataReceived too. I would also suggest you update the question to include at least adding of those handlers. Otherwise, you'll keep getting this same question in the comments :) – Alexandru Clonțea Jun 03 '22 at 11:24
  • 1
    Don't use `OutputDataReceived`, it reads data line by line, this program seems to output stream data, no line breaks. – shingo Jun 03 '22 at 11:24
  • @AlexandruClontea Nope, the process didn't write anything to `stderr`, checked with the handler... but, strangely enough setting `RedirectStandardError` to `true` and `RedirectStandardOutput` to `false` makes the process exit... but strangely, it doesn't work the other way! – zbigniew flet Jun 03 '22 at 11:29
  • @shingo Having a listener attached or not on `OutputDataReceived` doesn't seem to make a difference... the process doesn't exit either way :/ – zbigniew flet Jun 03 '22 at 11:32
  • Why are you redirecting stderr/stdout? Are you doing anything with the output? Sometimes things can get tricky with redirection, and sometimes that's because of something that's outside your control (something in the executable - see comments of https://stackoverflow.com/questions/22991115/read-process-standardoutput-before-new-line-received - good thinking @shingo). – Alexandru Clonțea Jun 03 '22 at 11:33
  • @AlexandruClonțea Yes, `yt-dlp` prints out a JSON object with the details of the given YouTube video to stdout (when the flag `--dump-json` is present, which it is in my case, see the code sample in the question), and if that doesn't work, it prints the error to stderr. I need to get the JSON output if the errorcode is 0, and display the error message otherwise. – zbigniew flet Jun 03 '22 at 11:35
  • @zbigniewflet I mean directly read data from `StandardOutput`. – shingo Jun 03 '22 at 11:38
  • @shingo After the Exited handler is fired, that's what I'm doing (`Process.StandardOutput.ReadToEnd()`), but that part of the code is irrelevant to the question as it isn't even called (the problem is that handler isn't firing, as the process doesn't even want to exit) – zbigniew flet Jun 03 '22 at 11:41
  • 1
    @zbigniewflet After? No. Maybe you need read [some examples](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.standardoutput?view=net-6.0). – shingo Jun 03 '22 at 11:44
  • 1
    You might get away with something like this https://stackoverflow.com/a/64449079/8695782 - some of the code is off, but you should get the point of it. Just be sure to steer clear of using ReadLine. Use .Read() or .ReadToEnd() instead. That should be enough for your usecase – Alexandru Clonțea Jun 03 '22 at 11:46
  • @shingo So, how am I supposed to know when the process will exit? Do I really have to create a whole thread to read the stream in a loop while checking if `Process.HasExited` is `true`? Is there a simpler and cleaner solution for this? – zbigniew flet Jun 03 '22 at 11:48
  • 1
    When `Exited` is fired. You saw the event was never fired probably because this `yt-dlp.exe` was waiting for you to read all its out data. – shingo Jun 03 '22 at 11:54

0 Answers0