OSDN Git Service

WinGui:
authorsr55 <sr55@b64f7644-9d1e-0410-96f1-a4d463321fa5>
Fri, 30 Apr 2010 20:24:05 +0000 (20:24 +0000)
committersr55 <sr55@b64f7644-9d1e-0410-96f1-a4d463321fa5>
Fri, 30 Apr 2010 20:24:05 +0000 (20:24 +0000)
- Some minor re factoring to the encode service.

git-svn-id: svn://localhost/HandBrake/trunk@3280 b64f7644-9d1e-0410-96f1-a4d463321fa5

win/C#/Services/Encode.cs
win/C#/Services/Queue.cs

index ba19f18..e4b9e65 100644 (file)
@@ -3,6 +3,8 @@
     Homepage: <http://handbrake.fr/>.\r
     It may be used under the terms of the GNU General Public License. */\r
 \r
+using System.Diagnostics.CodeAnalysis;\r
+\r
 namespace Handbrake.Services\r
 {\r
     using System;\r
@@ -62,9 +64,13 @@ namespace Handbrake.Services
         public IntPtr ProcessHandle { get; set; }\r
 \r
         /// <summary>\r
-        /// Gets or sets a value indicating whether HandBrakeCLI.exe is running\r
+        /// Gets or sets a value indicating whether IsEncoding.\r
         /// </summary>\r
-        public bool IsEncoding { get; set; }\r
+        public bool IsEncoding\r
+        {\r
+            get;\r
+            set;\r
+        }\r
 \r
         /// <summary>\r
         /// Gets or sets the Process ID\r
@@ -93,7 +99,7 @@ namespace Handbrake.Services
         /// </param>\r
         public void CreatePreviewSample(string query)\r
         {\r
-            this.Run(new Job {Query = query});\r
+            this.Run(new Job { Query = query });\r
         }\r
 \r
         /// <summary>\r
@@ -110,8 +116,6 @@ namespace Handbrake.Services
 \r
             if (this.EncodeEnded != null)\r
                 this.EncodeEnded(this, new EventArgs());\r
-\r
-            IsEncoding = false;\r
         }\r
 \r
         /// <summary>\r
@@ -131,8 +135,6 @@ namespace Handbrake.Services
 \r
             // HbProcess.StandardInput.AutoFlush = true;\r
             // HbProcess.StandardInput.WriteLine("^C");\r
-\r
-            IsEncoding = false;\r
         }\r
 \r
         /// <summary>\r
@@ -146,16 +148,20 @@ namespace Handbrake.Services
             this.job = encJob;\r
             try\r
             {\r
-                IsEncoding = true;\r
-\r
                 ResetLogReader();\r
 \r
+                if (this.EncodeStarted != null)\r
+                    this.EncodeStarted(this, new EventArgs());\r
+\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 strCmdLine = String.Format(@" /C """"{0}"" {1} 2>""{2}"" """, handbrakeCLIPath, encJob.Query, logPath);\r
+                string strCmdLine = String.Format(@" /C """"{0}"" {1} 2>""{2}"" """, handbrakeCLIPath, encJob.Query,\r
+                                                  logPath);\r
                 var cliStart = new ProcessStartInfo("CMD.exe", strCmdLine);\r
 \r
                 if (Settings.Default.enocdeStatusInGui)\r
@@ -169,44 +175,48 @@ namespace Handbrake.Services
                     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
+                Process startProcess = Process.Start(cliStart);\r
+\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
-                if (this.HbProcess != null)\r
-                    this.ProcessHandle = this.HbProcess.MainWindowHandle; // Set the process Handle\r
+                if (startProcess != null)\r
+                    this.ProcessHandle = startProcess.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
+                {\r
+                    HbProcess = Process.GetProcessById(this.ProcessID);\r
+                    HbProcess.EnableRaisingEvents = true;\r
+                    HbProcess.Exited += new EventHandler(HbProcess_Exited);\r
+                }\r
 \r
-                if (hbCliProcess != null)\r
+                if (HbProcess != null)\r
                     switch (Settings.Default.processPriority)\r
                     {\r
                         case "Realtime":\r
-                            hbCliProcess.PriorityClass = ProcessPriorityClass.RealTime;\r
+                            HbProcess.PriorityClass = ProcessPriorityClass.RealTime;\r
                             break;\r
                         case "High":\r
-                            hbCliProcess.PriorityClass = ProcessPriorityClass.High;\r
+                            HbProcess.PriorityClass = ProcessPriorityClass.High;\r
                             break;\r
                         case "Above Normal":\r
-                            hbCliProcess.PriorityClass = ProcessPriorityClass.AboveNormal;\r
+                            HbProcess.PriorityClass = ProcessPriorityClass.AboveNormal;\r
                             break;\r
                         case "Normal":\r
-                            hbCliProcess.PriorityClass = ProcessPriorityClass.Normal;\r
+                            HbProcess.PriorityClass = ProcessPriorityClass.Normal;\r
                             break;\r
                         case "Low":\r
-                            hbCliProcess.PriorityClass = ProcessPriorityClass.Idle;\r
+                            HbProcess.PriorityClass = ProcessPriorityClass.Idle;\r
                             break;\r
                         default:\r
-                            hbCliProcess.PriorityClass = ProcessPriorityClass.BelowNormal;\r
+                            HbProcess.PriorityClass = ProcessPriorityClass.BelowNormal;\r
                             break;\r
                     }\r
             }\r
@@ -222,6 +232,20 @@ namespace Handbrake.Services
         }\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
@@ -237,24 +261,25 @@ namespace Handbrake.Services
                     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
@@ -308,8 +333,6 @@ namespace Handbrake.Services
             if (this.EncodeEnded != null)\r
                 this.EncodeEnded(this, new EventArgs());\r
 \r
-            IsEncoding = false;\r
-\r
             if (!IsEncoding)\r
             {\r
                 windowTimer.Dispose();\r
@@ -356,7 +379,8 @@ namespace Handbrake.Services
         {\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
@@ -436,7 +460,8 @@ namespace Handbrake.Services
             {\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
index 4aba977..39437fc 100644 (file)
@@ -364,8 +364,6 @@ namespace Handbrake.Services
                 HbProcess.Close();\r
                 HbProcess.Dispose();\r
 \r
-                IsEncoding = false;\r
-\r
                 // Growl\r
                 if (Properties.Settings.Default.growlEncode)\r
                     GrowlCommunicator.Notify("Encode Completed", \r