From 43b29fa60d2d30aa1802a7b913436f86cfe3d73f Mon Sep 17 00:00:00 2001 From: sr55 Date: Wed, 13 May 2009 19:50:47 +0000 Subject: WinGui: - The CLI status information can now optionally be displayed in the encode status bar in the GUI. - Fixed Scan and Encode cancel functions with a hack. It was killing CMD.exe, not HandBrakeCLI.exe git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@2416 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- win/C#/Functions/Encode.cs | 36 ++++++++++++++++++++++++++++------ win/C#/Functions/Main.cs | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 6 deletions(-) (limited to 'win/C#/Functions') diff --git a/win/C#/Functions/Encode.cs b/win/C#/Functions/Encode.cs index 044fad359..a6bfd263a 100644 --- a/win/C#/Functions/Encode.cs +++ b/win/C#/Functions/Encode.cs @@ -19,29 +19,33 @@ namespace Handbrake.Functions private static extern void LockWorkStation(); [DllImport("user32.dll")] private static extern int ExitWindowsEx(int uFlags, int dwReason); - - // Declarations - Process hbProc = new Process(); - + /// /// Execute a HandBrakeCLI process. /// /// The CLI Query public Process runCli(string query) { + Process hbProc = new Process(); try { string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe"); string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs"; string logPath = Path.Combine(logDir, "last_encode_log.txt"); - string strCmdLine = String.Format(@" CMD /c """"{0}"" {1} 2>""{2}"" """, handbrakeCLIPath, query, logPath); ProcessStartInfo cliStart = new ProcessStartInfo("CMD.exe", strCmdLine); + if (Properties.Settings.Default.enocdeStatusInGui == "Checked") + { + cliStart.RedirectStandardOutput = true; + cliStart.UseShellExecute = false; + } if (Properties.Settings.Default.cli_minimized == "Checked") cliStart.WindowStyle = ProcessWindowStyle.Minimized; + + Process[] before = Process.GetProcesses(); // Get a list of running processes before starting. hbProc = Process.Start(cliStart); - processID = hbProc.Id; + processID = Main.getCliProcess(before); isEncoding = true; currentQuery = query; @@ -73,9 +77,29 @@ namespace Handbrake.Functions { MessageBox.Show("An error occured in runCli()\n Error Information: \n\n" + exc); } + return hbProc; } + /// + /// Kill the CLI process + /// + public void closeCLI() + { + Process[] prs = Process.GetProcesses(); + foreach (Process process in prs) + { + if (process.Id == processID) + { + process.Refresh(); + if (!process.HasExited) + process.Kill(); + + process.WaitForExit(); + } + } + } + /// /// Perform an action after an encode. e.g a shutdown, standby, restart etc. /// diff --git a/win/C#/Functions/Main.cs b/win/C#/Functions/Main.cs index d7161205f..a95f5e144 100644 --- a/win/C#/Functions/Main.cs +++ b/win/C#/Functions/Main.cs @@ -11,6 +11,7 @@ using System.Diagnostics; using System.Text.RegularExpressions; using System.Collections.Generic; using System.Xml.Serialization; +using System.Threading; namespace Handbrake.Functions { @@ -371,5 +372,52 @@ namespace Handbrake.Functions } } + /// + /// Get the Process ID of HandBrakeCLI for the current instance. + /// + /// List of processes before the new process was started + /// Int - Process ID + public static int getCliProcess(Process[] before) + { + // This is a bit of a cludge. Maybe someone has a better idea on how to impliment this. + // Since we used CMD to start HandBrakeCLI, we don't get the process ID from hbProc. + // Instead we take the processes before and after, and get the ID of HandBrakeCLI.exe + // avoiding any previous instances of HandBrakeCLI.exe in before. + // Kill the current process. + + Process[] hbProcesses = Process.GetProcessesByName("HandBrakeCLI"); + + // Another hack. We maybe need to wait a few seconds for HandBrakeCLI to launch + if (hbProcesses.Length == 0) + { + Thread.Sleep(2000); + hbProcesses = Process.GetProcessesByName("HandBrakeCLI"); + } + + Process hbProcess = null; + if (hbProcesses.Length > 0) + foreach (Process process in hbProcesses) + { + Boolean found = false; + // Check if the current CLI instance was running before we started the current one + foreach (Process bprocess in before) + { + if (process.Id == bprocess.Id) + found = true; + } + + // If it wasn't running before, we found the process we want. + if (!found) + { + hbProcess = process; + break; + } + } + if (hbProcess != null) + return hbProcess.Id; + + return -1; + } + } } -- cgit v1.2.3