0

I ran into a peculiar situation with the following code sample that I don't understand. Below is a complete program that starts a process of youtube-dl.exe that downloads a youtube video. Both youtube-dl and ffmpeg binaries are present in the same directory as Task_Starter.exe (my program).

const string destinationFolder = @"C:\Users\Home\source\repos\Task Starter\Task Starter\bin\Release";

Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.FileName = "youtube-dl.exe";
process.StartInfo.Arguments = "https://www.youtube.com/watch?v=K4TOrB7at0Y";
process.StartInfo.Arguments += (" -o " + "\"" + destinationFolder + "/%(title)s-%(id)s.%(ext)s" +
                                "\" --restrict-filenames --no-mtime --ffmpeg-location ffmpeg.exe");
Console.WriteLine("arguments: " + process.StartInfo.Arguments);
 
process.Start();
//Process.Start("notepad.exe");  // <--What is going on here?
 
Task.Run(() => {
    while (!process.StandardOutput.EndOfStream) {
       string line = process.StandardOutput.ReadLine();
       Console.WriteLine(line);
    }
 });

Console.ReadLine();

The strange part about it is that this code does not start the process when being run inside Rider but does exactly what it should when launching Task_Starter.exe in cmd.

It also does not start the video download when I run it via a task scheduler manually pressing Run button in Task Scheduler.

schtasks /Create /SC ONLOGON /TN "Youtube DL Starter" /RL HIGHEST /TR "'C:\Users\Home\source\repos\Task Starter\Task Starter\bin\Release\Task_Starter.exe'"

But it does start correctly if I just add Process.Start("notepad.exe"); anywhere below process.Start();

If I add that, then it successfully launches youtube-dl.exe, downloads the video both when executed within Rider and if executed via task.

I hope you can point out what I am missing here. How launching a completely separated process of notepad.exe influenced what other parts of my code do?

Windows 10 Pro, Version 10.0.18363 Build 18363 .NETFramework v4.6.1

GSerg
  • 76,472
  • 17
  • 159
  • 346
miran80
  • 945
  • 7
  • 22
  • 1
    What is the behavior if you synchronously do a StandardOutput.ReadToEnd instead of the Task? – Caius Jard Mar 20 '21 at 07:00
  • 1
    Apparently the extra delay introduced by starting the notepad gives the previously started process enough time to start up and generate output so that you can read it straight away without blocking on `ReadLine()`. Otherwise you reach `Console.ReadLine()` too early and become locked up (because you have a [fire and forget](https://stackoverflow.com/q/46053175/11683) on your task). – GSerg Mar 20 '21 at 13:57
  • @CaiusJard If I move while loop outside the Task.Run() it does work correctly without the need for the strange notepad startup. This could be related to what GSerg said as well. – miran80 Mar 20 '21 at 15:59

0 Answers0