处理等待退出无法正常工作

时间:2017-02-28 11:28:06

标签: c# asp.net process

我正在使用以下代码使用youtube-dl python脚本从youtube下载。

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();
    }
}


我以这种方式使用了proccess,因为我希望在我的客户端进度条中显示youtube-dl脚本的百分比。
但是存在一些问题,WaitForExit无效。我从其他主题中读到这个问题与正在进行的等待不适用于子进程有关(我的意思是在我的方式,等待退出适用于python不适用于youtube-dl脚本)
我该怎么办?

1 个答案:

答案 0 :(得分:1)

由于您对子进程感兴趣,或许您尝试使用该方法对youtube进程进行轮询:

Process.GetProcessesByName(string processName);

这样的事情:

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