2017-02-28 26 views
0

youtube-dl pythonスクリプトを使用してYouTubeからダウンロードするには、以下のコードを使用しています。Exit Wait For Exitが動作しない

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


私は私のクライアント側のプログレスバーで表示するためのユーチューブ-DLスクリプトの割合を持ちたいので、私はこの方法でproccessを使用。
しかし、いくつかの問題があり、それはWaitForExitが機能していないということです。私はこの問題が子プロセスのために働いていないことをプロセス中に待つことに関連していることを他の話題から読んでいます(自分のやり方では、pythonのexitがyoutube-dlスクリプトではないことを意味します)
どうすればよいですか?

+0

「ではないん何_exactly_どういう意味ですか? –

+0

@RenéVogtプロセスは終了するまで待機しません。 – parsa

答えて

1

あなたは多分メソッドを使用して、ユーチューブプロセスにポーリングしようと、子プロセスに興味があるので、このような

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); 
    } 
+0

私はあなたの言葉としてそれを行いますが、まだ動作しません。私は 'youtubeProcess'値を表示します、それは空です。 自分のコードを編集し、 'process.StartInfo.Arguments'.jpgを更新します。何が間違っているのか分かりますか? – parsa

+0

プロセス名は 'youtube-dl'ですか? (あなたはタスクマネージャに表示されるように名前をつけるべきです) – TeaHoney

+0

私はタスクマネージャを見て、私のプロセスに関連するプロセス名は** conhost.exe **と** python.exe **でした。 – parsa

関連する問題