OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / HandBrake.ApplicationServices / Services / Encode.cs
index 2e10656..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
@@ -51,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
@@ -173,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
@@ -347,7 +355,6 @@ namespace HandBrake.ApplicationServices.Services
         private void HbProcessExited(object sender, EventArgs e)\r
         {\r
             IsEncoding = false;\r
-  \r
             if (windowsSeven.IsWindowsSeven)\r
             {\r
                 windowsSeven.SetTaskBarProgressToNoProgress();\r
@@ -360,10 +367,23 @@ 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
@@ -479,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