2017-02-28 146 views
0

我使用下面的代碼從youtube下載使用youtube-dl python腳本。進程等待退出不起作用

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腳本)
我該怎麼辦?

+0

_exactly_做什麼?「是不是工作「的意思? –

+0

@RenéVogt該過程不會等到它結束。 – parsa

回答

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

我這樣做是因爲你提到,但仍然不行。我打印'youtubeProcess'值,它是空的。 我編輯我的代碼並更新'process.StartInfo.Arguments'.please看看它,如果你知道請說出了什麼問題? – parsa

+0

你確定進程名是:'youtube-dl'? (你應該像任務管理器中顯示的名稱) – TeaHoney

+0

我看看任務管理器,與我的進程相關的進程名稱有** conhost.exe **和** python.exe **。 – parsa