Homepage: <http://handbrake.fr/>.\r
It may be used under the terms of the GNU General Public License. */\r
\r
-\r
namespace Handbrake.Services\r
{\r
using System;\r
/// </summary>\r
public class Encode\r
{\r
+ /* Private Variables */\r
+\r
/// <summary>\r
/// An Encode Job\r
/// </summary>\r
private Timer windowTimer;\r
\r
/// <summary>\r
+ /// Gets The Process Handle\r
+ /// </summary>\r
+ private IntPtr processHandle;\r
+\r
+ /// <summary>\r
+ /// Gets the Process ID\r
+ /// </summary>\r
+ private int processID;\r
+\r
+ /* Event Handlers */\r
+\r
+ /// <summary>\r
/// Fires when a new CLI Job starts\r
/// </summary>\r
public event EventHandler EncodeStarted;\r
/// </summary>\r
public event EventHandler EncodeEnded;\r
\r
+ /* Properties */\r
+\r
/// <summary>\r
/// Gets or sets The HB Process\r
/// </summary>\r
public Process HbProcess { get; set; }\r
\r
/// <summary>\r
- /// Gets or sets The Process Handle\r
- /// </summary>\r
- public IntPtr ProcessHandle { get; set; }\r
-\r
- /// <summary>\r
- /// Gets or sets a value indicating whether HandBrakeCLI.exe is running\r
+ /// Gets a value indicating whether IsEncoding.\r
/// </summary>\r
- public bool IsEncoding { get; set; }\r
+ public bool IsEncoding { get; private set; }\r
\r
- /// <summary>\r
- /// Gets or sets the Process ID\r
- /// </summary>\r
- public int ProcessID { get; set; }\r
+ /* Public Methods */\r
\r
/// <summary>\r
/// Gets ActivityLog.\r
{\r
get\r
{\r
- if (logBuffer != null)\r
- return logBuffer.ToString();\r
+ if (logBuffer == null)\r
+ {\r
+ ResetLogReader();\r
+ ReadFile(null);\r
+ }\r
\r
- return string.Empty;\r
+ return logBuffer != null ? logBuffer.ToString() : string.Empty;\r
}\r
}\r
\r
/// </param>\r
public void CreatePreviewSample(string query)\r
{\r
- Job job = new Job {Query = query};\r
- this.Run(job);\r
+ this.Run(new Job { Query = query }, true);\r
}\r
\r
/// <summary>\r
\r
if (this.EncodeEnded != null)\r
this.EncodeEnded(this, new EventArgs());\r
-\r
- IsEncoding = false;\r
}\r
\r
/// <summary>\r
/// </summary>\r
public void SafelyClose()\r
{\r
- if ((int)this.ProcessHandle == 0)\r
+ if ((int)this.processHandle == 0)\r
return;\r
\r
// Allow the CLI to exit cleanly\r
- Win32.SetForegroundWindow((int)this.ProcessHandle);\r
+ Win32.SetForegroundWindow((int)this.processHandle);\r
SendKeys.Send("^C");\r
SendKeys.Flush();\r
\r
// HbProcess.StandardInput.AutoFlush = true;\r
// HbProcess.StandardInput.WriteLine("^C");\r
-\r
- IsEncoding = false;\r
}\r
\r
/// <summary>\r
/// <param name="encJob">\r
/// The enc Job.\r
/// </param>\r
- protected void Run(Job encJob)\r
+ /// <param name="RequireStandardOuput">\r
+ /// Set to True to show no window and force standard output redirect\r
+ /// </param>\r
+ protected void Run(Job encJob, bool RequireStandardOuput)\r
{\r
this.job = encJob;\r
try\r
{\r
- IsEncoding = true;\r
-\r
ResetLogReader();\r
+ IsEncoding = true;\r
\r
string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
- string logPath =\r
- Path.Combine(\r
- Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs",\r
- "last_encode_log.txt");\r
+ string logPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs", "last_encode_log.txt");\r
string strCmdLine = String.Format(@" /C """"{0}"" {1} 2>""{2}"" """, handbrakeCLIPath, encJob.Query, logPath);\r
var cliStart = new ProcessStartInfo("CMD.exe", strCmdLine);\r
\r
- if (Settings.Default.enocdeStatusInGui)\r
+ if (Settings.Default.enocdeStatusInGui || RequireStandardOuput)\r
{\r
cliStart.RedirectStandardOutput = true;\r
cliStart.UseShellExecute = false;\r
- if (!Settings.Default.showCliForInGuiEncodeStatus)\r
+ if (!Settings.Default.showCliForInGuiEncodeStatus || RequireStandardOuput)\r
cliStart.CreateNoWindow = true;\r
}\r
if (Settings.Default.cli_minimized)\r
cliStart.WindowStyle = ProcessWindowStyle.Minimized;\r
\r
Process[] before = Process.GetProcesses(); // Get a list of running processes before starting.\r
- this.HbProcess = Process.Start(cliStart);\r
- this.ProcessID = Main.GetCliProcess(before);\r
-\r
- // Fire the Encode Started Event\r
- if (this.EncodeStarted != null)\r
- this.EncodeStarted(this, new EventArgs());\r
-\r
-\r
- if (this.HbProcess != null)\r
- this.ProcessHandle = this.HbProcess.MainWindowHandle; // Set the process Handle\r
+ HbProcess = Process.Start(cliStart);\r
+ this.processID = Main.GetCliProcess(before);\r
\r
+ if (HbProcess != null)\r
+ this.processHandle = HbProcess.MainWindowHandle; // Set the process Handle\r
+ \r
// Start the Log Monitor\r
windowTimer = new Timer(new TimerCallback(ReadFile), null, 1000, 1000);\r
\r
// Set the process Priority\r
Process hbCliProcess = null;\r
- if (this.ProcessID != -1)\r
- hbCliProcess = Process.GetProcessById(this.ProcessID);\r
+ if (this.processID != -1)\r
+ {\r
+ hbCliProcess = Process.GetProcessById(this.processID);\r
+ hbCliProcess.EnableRaisingEvents = true;\r
+ hbCliProcess.Exited += new EventHandler(HbProcess_Exited);\r
+ }\r
\r
if (hbCliProcess != null)\r
switch (Settings.Default.processPriority)\r
hbCliProcess.PriorityClass = ProcessPriorityClass.BelowNormal;\r
break;\r
}\r
+\r
+ // Fire the Encode Started Event\r
+ if (this.EncodeStarted != null)\r
+ this.EncodeStarted(this, new EventArgs());\r
}\r
catch (Exception exc)\r
{\r
}\r
\r
/// <summary>\r
+ /// The HandBrakeCLI process has exited.\r
+ /// </summary>\r
+ /// <param name="sender">\r
+ /// The sender.\r
+ /// </param>\r
+ /// <param name="e">\r
+ /// The EventArgs.\r
+ /// </param>\r
+ private void HbProcess_Exited(object sender, EventArgs e)\r
+ {\r
+ IsEncoding = false;\r
+ }\r
+\r
+ /// <summary>\r
/// Function to run the CLI directly rather than via CMD\r
/// TODO: Code to handle the Log data has yet to be written.\r
/// TODO: Code to handle the % / ETA info has to be written.\r
this.EncodeStarted(this, new EventArgs());\r
\r
IsEncoding = true;\r
+\r
ResetLogReader();\r
\r
// Setup the job\r
string handbrakeCLIPath = Path.Combine(Environment.CurrentDirectory, "HandBrakeCLI.exe");\r
HbProcess = new Process\r
- {\r
- StartInfo =\r
- {\r
- FileName = handbrakeCLIPath,\r
- Arguments = query,\r
- UseShellExecute = false,\r
- RedirectStandardOutput = true,\r
- RedirectStandardError = true,\r
- RedirectStandardInput = true,\r
- CreateNoWindow = false,\r
- WindowStyle = ProcessWindowStyle.Minimized\r
- }\r
- };\r
+ {\r
+ StartInfo =\r
+ {\r
+ FileName = handbrakeCLIPath,\r
+ Arguments = query,\r
+ UseShellExecute = false,\r
+ RedirectStandardOutput = true,\r
+ RedirectStandardError = true,\r
+ RedirectStandardInput = true,\r
+ CreateNoWindow = false,\r
+ WindowStyle = ProcessWindowStyle.Minimized\r
+ }\r
+ };\r
\r
// Setup event handlers for rediected data\r
HbProcess.ErrorDataReceived += new DataReceivedEventHandler(HbProcErrorDataReceived);\r
}\r
\r
// Set the class items\r
- this.ProcessID = HbProcess.Id;\r
- this.ProcessHandle = HbProcess.Handle;\r
+ this.processID = HbProcess.Id;\r
+ this.processHandle = HbProcess.Handle;\r
}\r
catch (Exception exc)\r
{\r
/// </summary>\r
protected void Finish()\r
{\r
- if (this.EncodeEnded != null)\r
- this.EncodeEnded(this, new EventArgs());\r
-\r
- IsEncoding = false;\r
-\r
if (!IsEncoding)\r
{\r
windowTimer.Dispose();\r
ReadFile(null);\r
}\r
\r
+ if (this.EncodeEnded != null)\r
+ this.EncodeEnded(this, new EventArgs());\r
+\r
// Growl\r
if (Settings.Default.growlQueue)\r
GrowlCommunicator.Notify("Queue Completed", "Put down that cocktail...\nyour Handbrake queue is done.");\r
{\r
try\r
{\r
- string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
+ string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +\r
+ "\\HandBrake\\logs";\r
string logPath = Path.Combine(logDir, "last_encode_log.txt");\r
\r
var reader = new StreamReader(File.Open(logPath, FileMode.Open, FileAccess.Read, FileShare.Read));\r
{\r
// last_encode_log.txt is the primary log file. Since .NET can't read this file whilst the CLI is outputing to it (Not even in read only mode),\r
// we'll need to make a copy of it.\r
- string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
+ string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +\r
+ "\\HandBrake\\logs";\r
string logFile = Path.Combine(logDir, "last_encode_log.txt");\r
string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");\r
\r
}\r
\r
// Put the Query and User Generated Query Flag on the log.\r
- if (logFilePosition == 0)\r
+ if (logFilePosition == 0 && job.Query != null)\r
{\r
logBuffer.AppendLine("### CLI Query: " + job.Query);\r
logBuffer.AppendLine("### User Query: " + job.CustomQuery);\r