OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / HandBrake.ApplicationServices / Services / Encode.cs
index 90c224a..4a6333d 100644 (file)
@@ -6,6 +6,7 @@
 namespace HandBrake.ApplicationServices.Services\r
 {\r
     using System;\r
+    using System.ComponentModel;\r
     using System.Diagnostics;\r
     using System.IO;\r
     using System.Text;\r
@@ -17,6 +18,7 @@ namespace HandBrake.ApplicationServices.Services
     using HandBrake.ApplicationServices.Model;\r
     using HandBrake.ApplicationServices.Parsing;\r
     using HandBrake.ApplicationServices.Services.Interfaces;\r
+    using HandBrake.ApplicationServices.Utilities;\r
 \r
     /// <summary>\r
     /// Class which handles the CLI\r
@@ -50,6 +52,11 @@ namespace HandBrake.ApplicationServices.Services
         /// </summary>\r
         private Win7 windowsSeven = new Win7();\r
 \r
+        /// <summary>\r
+        /// A Lock for the filewriter\r
+        /// </summary>\r
+        static readonly object fileWriterLock = new object();\r
+\r
         #endregion\r
 \r
         /// <summary>\r
@@ -131,9 +138,9 @@ namespace HandBrake.ApplicationServices.Services
         {\r
             try\r
             {\r
-                QueueTask QueueTask = encodeQueueTask as QueueTask;\r
+                QueueTask queueTask = encodeQueueTask;\r
 \r
-                if (QueueTask == null)\r
+                if (queueTask == null)\r
                 {\r
                     throw new ArgumentNullException("QueueTask was null");\r
                 }\r
@@ -147,14 +154,14 @@ namespace HandBrake.ApplicationServices.Services
 \r
                 if (enableLogging)\r
                 {\r
-                    if (!SetupLogging(QueueTask))\r
+                    try\r
                     {\r
-                        if (this.EncodeCompleted != null)\r
-                            this.EncodeCompleted(\r
-                                this,\r
-                                new EncodeCompletedEventArgs(false, null, "Unable to Start Encode Logging process."));\r
-\r
-                        return;\r
+                        SetupLogging(queueTask);\r
+                    }\r
+                    catch (Exception)\r
+                    {\r
+                        IsEncoding = false;\r
+                        throw;\r
                     }\r
                 }\r
 \r
@@ -164,7 +171,7 @@ namespace HandBrake.ApplicationServices.Services
                 }\r
 \r
                 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
-                ProcessStartInfo cliStart = new ProcessStartInfo(handbrakeCLIPath, QueueTask.Query)\r
+                ProcessStartInfo cliStart = new ProcessStartInfo(handbrakeCLIPath, queueTask.Query)\r
                 {\r
                     RedirectStandardOutput = true,\r
                     RedirectStandardError = enableLogging ? true : false,\r
@@ -172,7 +179,9 @@ namespace HandBrake.ApplicationServices.Services
                     CreateNoWindow = !Init.ShowCliForInGuiEncodeStatus ? true : false\r
                 };\r
 \r
-                this.HbProcess = Process.Start(cliStart);\r
+                this.HbProcess = new Process { StartInfo = cliStart };\r
+\r
+                this.HbProcess.Start();\r
 \r
                 if (enableLogging)\r
                 {\r
@@ -288,10 +297,6 @@ namespace HandBrake.ApplicationServices.Services
             //}*/\r
         }\r
 \r
-        #endregion\r
-\r
-        #region Private Helper Methods\r
-\r
         /// <summary>\r
         /// Save a copy of the log to the users desired location or a default location\r
         /// if this feature is enabled in options.\r
@@ -299,7 +304,7 @@ namespace HandBrake.ApplicationServices.Services
         /// <param name="destination">\r
         /// The Destination File Path\r
         /// </param>\r
-        protected void CopyLog(string destination)\r
+        public void ProcessLogs(string destination)\r
         {\r
             try\r
             {\r
@@ -334,6 +339,10 @@ namespace HandBrake.ApplicationServices.Services
             }\r
         }\r
 \r
+        #endregion\r
+\r
+        #region Private Helper Methods\r
+\r
         /// <summary>\r
         /// The HandBrakeCLI process has exited.\r
         /// </summary>\r
@@ -346,9 +355,6 @@ namespace HandBrake.ApplicationServices.Services
         private void HbProcessExited(object sender, EventArgs e)\r
         {\r
             IsEncoding = false;\r
-            if (this.EncodeCompleted != null)\r
-                this.EncodeCompleted(this, new EncodeCompletedEventArgs(true, null, string.Empty));\r
-\r
             if (windowsSeven.IsWindowsSeven)\r
             {\r
                 windowsSeven.SetTaskBarProgressToNoProgress();\r
@@ -361,16 +367,32 @@ namespace HandBrake.ApplicationServices.Services
 \r
             try\r
             {\r
-                if (fileWriter != null)\r
+                lock (fileWriterLock)\r
                 {\r
-                    fileWriter.Close();\r
-                    fileWriter.Dispose();\r
+                    // This is just a quick hack to ensure that we are done processing the logging data.\r
+                    // Logging data comes in after the exit event has processed sometimes. We should really impliment ISyncronizingInvoke\r
+                    // and set the SyncObject on the process. I think this may resolve this properly.\r
+                    // For now, just wait 2.5 seconds to let any trailing log messages come in and be processed.\r
+                    Thread.Sleep(2500);\r
+\r
+                    this.HbProcess.CancelErrorRead();\r
+\r
+                    if (fileWriter != null)\r
+                    {\r
+                        fileWriter.Close();\r
+                        fileWriter.Dispose();\r
+                    }\r
+\r
+                    fileWriter = null;\r
                 }\r
             }\r
             catch (Exception exc)\r
             {\r
                 // This exception doesn't warrent user interaction, but it should be logged (TODO)\r
             }\r
+\r
+            if (this.EncodeCompleted != null)\r
+                this.EncodeCompleted(this, new EncodeCompletedEventArgs(true, null, string.Empty));\r
         }\r
 \r
         /// <summary>\r
@@ -429,10 +451,7 @@ namespace HandBrake.ApplicationServices.Services
         /// <param name="encodeQueueTask">\r
         /// The encode QueueTask.\r
         /// </param>\r
-        /// <returns>\r
-        /// The setup logging.\r
-        /// </returns>\r
-        private bool SetupLogging(QueueTask encodeQueueTask)\r
+        private void SetupLogging(QueueTask encodeQueueTask)\r
         {\r
             string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
             string logFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", Init.InstanceId));\r
@@ -448,20 +467,17 @@ namespace HandBrake.ApplicationServices.Services
 \r
                 fileWriter = new StreamWriter(logFile) { AutoFlush = true };\r
 \r
-                fileWriter.WriteLine(Logging.CreateCliLogHeader(encodeQueueTask));\r
-                logBuffer.AppendLine(Logging.CreateCliLogHeader(encodeQueueTask));\r
-\r
-                return true;\r
+                fileWriter.WriteLine(UtilityService.CreateCliLogHeader(encodeQueueTask));\r
+                logBuffer.AppendLine(UtilityService.CreateCliLogHeader(encodeQueueTask));\r
             }\r
-            catch (Exception exc)\r
+            catch (Exception)\r
             {\r
                 if (fileWriter != null)\r
                 {\r
                     fileWriter.Close();\r
                     fileWriter.Dispose();\r
                 }\r
-\r
-                return false;\r
+                throw;\r
             }\r
         }\r
 \r
@@ -483,21 +499,26 @@ namespace HandBrake.ApplicationServices.Services
                     lock (logBuffer)\r
                         logBuffer.AppendLine(e.Data);\r
 \r
-                    if (fileWriter != null && fileWriter.BaseStream.CanWrite)\r
+                    lock (fileWriterLock)\r
                     {\r
-                        fileWriter.WriteLine(e.Data);\r
-\r
-                        // If the logging grows past 100MB, kill the encode and stop.\r
-                        if (fileWriter.BaseStream.Length > 100000000)\r
+                        if (fileWriter != null && fileWriter.BaseStream.CanWrite)\r
                         {\r
-                            this.Stop(new Exception("The encode has been stopped. The log file has grown to over 100MB which indicates a serious problem has occured with the encode." +\r
-                                "Please check the encode log for an indication of what the problem is."));\r
+                            fileWriter.WriteLine(e.Data);\r
+\r
+                            // If the logging grows past 100MB, kill the encode and stop.\r
+                            if (fileWriter.BaseStream.Length > 100000000)\r
+                            {\r
+                                this.Stop(\r
+                                    new Exception(\r
+                                        "The encode has been stopped. The log file has grown to over 100MB which indicates a serious problem has occured with the encode." +\r
+                                        "Please check the encode log for an indication of what the problem is."));\r
+                            }\r
                         }\r
                     }\r
                 }\r
                 catch (Exception exc)\r
                 {\r
-                    // errorService.ShowError("Unable to write log data...", exc.ToString());\r
+                    // Do Nothing.\r
                 }\r
             }\r
         }\r