Parsa Saei
Parsa Saei

Reputation: 1443

Process Wait For Exit not work

I'm using the below code to download from youtube using youtube-dl python script.

string pythonPath = @"C:\Python35\python.exe";
string ydl = @"C:\Y\ydl\youtube-dl";
string tempLocation = Server.MapPath("/ydl/");

string Output = "";
string Error = "";

int numOutputLines = 0;
int numErrorLines = 0;

using (Process process = new Process())
{
    process.EnableRaisingEvents = true;
    process.StartInfo.ErrorDialog = false;
    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.FileName = pythonPath;
    process.StartInfo.WorkingDirectory = tempLocation;
    process.StartInfo.Arguments = ydl + " --output test.mp4 --force-ipv4 -f bestvideo[ext=mp4]+bestaudio[ext=m4a] \"" + Url + "\"";
    process.StartInfo.Verb = "runas";
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.CreateNoWindow = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardError = true;

    StringBuilder output = new StringBuilder();
    StringBuilder error = new StringBuilder();

    using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
    using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
    {
        process.OutputDataReceived += (sender, e) =>
        {
            if (e.Data == null)
            {
                outputWaitHandle.Set();
            }
            else
            {
                numOutputLines++;
                this.Context.Response.Write(Environment.NewLine + "[" + numOutputLines.ToString() + "] - " + e.Data);
                output.AppendLine("[" + numOutputLines.ToString() + "] - " + e.Data);
            }
        };
        process.ErrorDataReceived += (sender, e) =>
        {
            if (e.Data == null)
            {
                errorWaitHandle.Set();
            }
            else
            {
                numErrorLines++;
                this.Context.Response.Write(Environment.NewLine + "[" + numErrorLines.ToString() + "] - " + e.Data);
                error.AppendLine("[" + numErrorLines.ToString() + "] - " + e.Data);
            }
        };

        //process.Exited += (s, a) =>
        //{
        //    process.Close();
        //};

        process.Start();

        process.BeginOutputReadLine();
        process.BeginErrorReadLine();

            //process.WaitForExit();
            Process[] curProcess = Process.GetProcessesByName("youtube-dl");
            Process youtubeProcess = curProcess.FirstOrDefault();  

            while (!youtubeProcess.HasExited)
            {
                Thread.Sleep(100);
            }

        Output = output.ToString();
        Error = error.ToString();
        process.Close();
    }
}


I used the proccess in this way because I want to have the percentage of youtube-dl script for showing in my client side progress bar.
But there are some problems and it's that WaitForExit is not working. I read from other topics that this issue is related to wait in process not working for child process(I mean in my way, the wait for exit works for python not for youtube-dl script)
What should I do?

Upvotes: 0

Views: 1257

Answers (1)

TeaHoney
TeaHoney

Reputation: 150

Since you are interested in a child process maybe you an try to poll on the youtube process by using the method:

Process.GetProcessesByName(string processName);

Something like this:

   Process[] curProcess = Process.GetProcessesByName("your youtube process name");
   Process youtubeProcess = curProcess.FirstOrDefault();  // Get here the right process instance
   while (!youtubeProcess.HasExited)
   {
       Thread.Sleep(100);
   }

Upvotes: 1

Related Questions