diff --git a/YTDLNetFrontEnd/YTDLNetFrontEnd/Main.Designer.cs b/YTDLNetFrontEnd/YTDLNetFrontEnd/Main.Designer.cs index 3eb14cc..a37f787 100644 --- a/YTDLNetFrontEnd/YTDLNetFrontEnd/Main.Designer.cs +++ b/YTDLNetFrontEnd/YTDLNetFrontEnd/Main.Designer.cs @@ -120,7 +120,7 @@ this.buttonInstall.Margin = new System.Windows.Forms.Padding(0); this.buttonInstall.Name = "buttonInstall"; this.buttonInstall.Size = new System.Drawing.Size(142, 24); - this.buttonInstall.TabIndex = 4; + this.buttonInstall.TabIndex = 1; this.buttonInstall.Text = "Install"; this.buttonInstall.UseVisualStyleBackColor = true; this.buttonInstall.Click += new System.EventHandler(this.buttonInstall_Click); @@ -132,7 +132,7 @@ this.buttonExit.Margin = new System.Windows.Forms.Padding(0); this.buttonExit.Name = "buttonExit"; this.buttonExit.Size = new System.Drawing.Size(142, 24); - this.buttonExit.TabIndex = 5; + this.buttonExit.TabIndex = 2; this.buttonExit.Text = "Exit"; this.buttonExit.UseVisualStyleBackColor = true; this.buttonExit.Click += new System.EventHandler(this.buttonExit_Click); diff --git a/YTDLNetFrontEnd/YTDLNetFrontEnd/Main.cs b/YTDLNetFrontEnd/YTDLNetFrontEnd/Main.cs index 232d256..189de65 100644 --- a/YTDLNetFrontEnd/YTDLNetFrontEnd/Main.cs +++ b/YTDLNetFrontEnd/YTDLNetFrontEnd/Main.cs @@ -55,25 +55,34 @@ namespace com.captainalm.YTDLNetFrontEnd Environment.Exit(0); } + private void setEnableButtons(bool value) + { + this.Invoke(new Action(() => + { + buttonExit.Enabled = value; + buttonInstall.Enabled = value; + buttonGo.Enabled = value; + })); + } + private void backgroundWorkerMain_DoWork(object sender, DoWorkEventArgs e) { switch ((BWArg)e.Argument) { case BWArg.Install: - this.Invoke(new Action(() => { - buttonExit.Enabled = false; - buttonInstall.Enabled = false; - })); + setEnableButtons(false); var loctxt = YTDL.executeInstall((YTDL.getInstalled() != ApplicationType.Unavailable) ? YTDL.getInstalled() : (((Control.ModifierKeys & Keys.Shift) == Keys.Shift) ? ApplicationType.YoutubeDL : ApplicationType.YT_DLP), YTDL.getInstalled() != ApplicationType.Unavailable) + Environment.NewLine; + this.Invoke(new Action(() => { - textBoxOutput.Text += loctxt; + textBoxOutput.AppendText(loctxt); })); + if (YTDL.getInstalled() != ApplicationType.Unavailable) { this.Invoke(new Action(() => @@ -82,18 +91,11 @@ namespace com.captainalm.YTDLNetFrontEnd buttonInstall.Text = "Update"; })); } - this.Invoke(new Action(() => - { - buttonInstall.Enabled = true; - buttonExit.Enabled = true; - })); + + setEnableButtons(true); break; case BWArg.Go: - this.Invoke(new Action(() => - { - buttonExit.Enabled = false; - buttonGo.Enabled = false; - })); + setEnableButtons(false); if (theProcess != null) { @@ -106,6 +108,7 @@ namespace com.captainalm.YTDLNetFrontEnd this.Invoke(new Action(() => { theTarget = textBoxEntry.Text; + textBoxEntry.Text = ""; })); theProcess = YTDL.executeApplication(theTarget); @@ -119,11 +122,8 @@ namespace com.captainalm.YTDLNetFrontEnd theProcess.EnableRaisingEvents = true; theProcess.WaitForExit(); } - this.Invoke(new Action(() => - { - buttonGo.Enabled = true; - buttonExit.Enabled = true; - })); + + setEnableButtons(true); break; default: break; @@ -132,24 +132,37 @@ namespace com.captainalm.YTDLNetFrontEnd void theProcess_Exited(object sender, EventArgs e) { - theProcess.CancelOutputRead(); - theProcess.CancelErrorRead(); + try + { + theProcess.CancelOutputRead(); + } + catch (InvalidOperationException ex) + { + } + try + { + theProcess.CancelErrorRead(); + } + catch (InvalidOperationException ex) + { + } } void theProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e) { - if (e.Data.Equals("")) return; + if (e.Data == null || e.Data.Equals("")) return; this.Invoke(new Action(() => { - textBoxOutput.Text += "Error: " + e.Data + Environment.NewLine; + textBoxOutput.AppendText("Error: " + e.Data + Environment.NewLine); })); } void theProcess_OutputDataReceived(object sender, DataReceivedEventArgs e) { + if (e.Data == null || e.Data.Equals("")) return; this.Invoke(new Action(() => { - textBoxOutput.Text += e.Data + Environment.NewLine; + textBoxOutput.AppendText(e.Data + Environment.NewLine); })); } diff --git a/YTDLNetFrontEnd/YTDLNetFrontEnd/Properties/AssemblyInfo.cs b/YTDLNetFrontEnd/YTDLNetFrontEnd/Properties/AssemblyInfo.cs index 60e1b50..9f2c06d 100644 --- a/YTDLNetFrontEnd/YTDLNetFrontEnd/Properties/AssemblyInfo.cs +++ b/YTDLNetFrontEnd/YTDLNetFrontEnd/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.0.0.0")] -[assembly: AssemblyFileVersion("0.0.0.0")] +[assembly: AssemblyVersion("0.1.0.0")] +[assembly: AssemblyFileVersion("0.1.0.0")] diff --git a/YTDLNetFrontEnd/YTDLNetFrontEnd/YTDL.cs b/YTDLNetFrontEnd/YTDLNetFrontEnd/YTDL.cs index 1791c8e..779b384 100644 --- a/YTDLNetFrontEnd/YTDLNetFrontEnd/YTDL.cs +++ b/YTDLNetFrontEnd/YTDLNetFrontEnd/YTDL.cs @@ -46,26 +46,14 @@ namespace com.captainalm.YTDLNetFrontEnd default: return "Invalid Package Name"; } - var pipProSet = new ProcessStartInfo(findExecutableInPath("python"), "-m pip install " + packageName + ((update) ? " --upgrade" : "")) { UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true }; + var pipProSet = new ProcessStartInfo(findExecutableInPath("python"), "-m pip install " + packageName + ((update) ? " --upgrade" : "")) + { UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true, StandardOutputEncoding = Encoding.UTF8, StandardErrorEncoding = Encoding.UTF8 }; using (var pipPro = Process.Start(pipProSet)) { - using (var errTSR = new ThreadedStreamReader(pipPro.StandardError.BaseStream)) - { - using (var outTSR = new ThreadedStreamReader(pipPro.StandardOutput.BaseStream)) - { - pipPro.WaitForExit(); - try - { - var terr = System.Text.Encoding.UTF8.GetString(errTSR.getData()); - var tout = System.Text.Encoding.UTF8.GetString(outTSR.getData()); - if (terr != "") return terr; else return tout; - } - catch (Exception e) - { - return "Exception:" + e.GetType().FullName + ":" + e.Message; - } - } - } + var rpote = new ReadProcessOutputToEnd(pipPro); + pipPro.WaitForExit(); + if (!rpote.getError().Equals("")) return rpote.getError(); + return rpote.getOutput(); } } @@ -76,11 +64,13 @@ namespace com.captainalm.YTDLNetFrontEnd { try { - var pipProSet = new ProcessStartInfo(findExecutableInPath("python"), "-m pip freeze") {UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true}; + var pipProSet = new ProcessStartInfo(findExecutableInPath("python"), "-m pip freeze") + { UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true, StandardOutputEncoding = Encoding.UTF8, StandardErrorEncoding = Encoding.UTF8 }; using (var pipPro = Process.Start(pipProSet)) { - var theList = pipPro.StandardOutput.ReadToEnd(); + var rpote = new ReadProcessOutputToEnd(pipPro); pipPro.WaitForExit(); + var theList = rpote.getOutput(); if (theList.Contains("yt-dlp")) installed = ApplicationType.YT_DLP; else if (theList.Contains("youtube-dl")) installed = ApplicationType.YoutubeDL; else installed = ApplicationType.Unavailable; @@ -114,44 +104,67 @@ namespace com.captainalm.YTDLNetFrontEnd YT_DLP = 2 } - class ThreadedStreamReader : IDisposable + class ReadProcessOutputToEnd : IDisposable { - Thread theThread; - MemoryStream msToRet; - Stream theStream; - public ThreadedStreamReader(Stream streamIn) + Process theProcess; + string tout = ""; + string terr = ""; + + + public ReadProcessOutputToEnd(Process processIn) { - theStream = streamIn; - msToRet = new MemoryStream(); - theThread = new Thread(thread_execute); - theThread.IsBackground = true; - theThread.Start(); + theProcess = processIn; + theProcess.BeginOutputReadLine(); + theProcess.BeginErrorReadLine(); + theProcess.OutputDataReceived += theProcess_OutputDataReceived; + theProcess.ErrorDataReceived += theProcess_ErrorDataReceived; + theProcess.Exited += theProcess_Exited; + theProcess.EnableRaisingEvents = true; } - void thread_execute() + void theProcess_Exited(object sender, EventArgs e) { try { - int b; - while ((b = theStream.ReadByte()) != -1) msToRet.WriteByte((byte)b); + theProcess.CancelOutputRead(); } - catch (Exception e) + catch (InvalidOperationException ex) + { + } + try + { + theProcess.CancelErrorRead(); + } + catch (InvalidOperationException ex) { - var msg = System.Text.Encoding.UTF8.GetBytes("Exception:" + e.GetType().FullName + ":" + e.Message); - msToRet.Write(msg, 0, msg.Length); } } - public byte[] getData() + void theProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e) { - theThread.Join(); - return msToRet.ToArray(); + if (e.Data == null || e.Data.Equals("")) return; + terr += e.Data + Environment.NewLine; + } + + void theProcess_OutputDataReceived(object sender, DataReceivedEventArgs e) + { + if (e.Data == null || e.Data.Equals("")) return; + tout += e.Data + Environment.NewLine; + } + + public string getOutput() + { + return tout; + } + + public string getError() + { + return terr; } public void Dispose() { - theThread.Join(); - msToRet.Close(); + theProcess.Close(); } } }