]> granicus.if.org Git - handbrake/commitdiff
WinGui: Make the disposal of services more aggressive. May Fix or help in #1851
authorsr55 <sr55.hb@outlook.com>
Sat, 2 Feb 2019 10:56:34 +0000 (10:56 +0000)
committersr55 <sr55.hb@outlook.com>
Tue, 5 Feb 2019 19:43:33 +0000 (19:43 +0000)
win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs
win/CS/HandBrakeWPF/Services/PrePostActionService.cs
win/CS/HandBrakeWPF/Services/Scan/Interfaces/IScan.cs
win/CS/HandBrakeWPF/Services/Scan/LibScan.cs

index 382dd7cf97090483120faba738054674548a8720..ca4100b6277f2004e88aafe05dc8df07e91ef69c 100644 (file)
@@ -143,6 +143,10 @@ namespace HandBrakeWPF.Services.Encode
                 if (this.instance != null)
                 {
                     this.instance.StopEncode();
+                    this.instance.EncodeCompleted -= this.InstanceEncodeCompleted;
+                    this.instance.EncodeProgress -= this.InstanceEncodeProgress;
+                    this.instance.Dispose();
+                    this.instance = null;
                     this.ServiceLogMessage("Encode Stopped");
                 }
             }
@@ -209,11 +213,24 @@ namespace HandBrakeWPF.Services.Encode
             string hbLog = this.ProcessLogs(this.currentTask.Destination, this.isPreviewInstance, this.currentConfiguration);
             long filesize = this.GetFilesize(this.currentTask.Destination);
 
-            // Raise the Encode Completed EVent.
+            // Raise the Encode Completed Event.
             this.InvokeEncodeCompleted(
                 e.Error
                     ? new EventArgs.EncodeCompletedEventArgs(false, null, string.Empty, this.currentTask.Destination, hbLog, filesize)
                     : new EventArgs.EncodeCompletedEventArgs(true, null, string.Empty, this.currentTask.Destination, hbLog, filesize));
+
+            // Cleanup
+            try
+            {
+                this.instance.EncodeCompleted -= this.InstanceEncodeCompleted;
+                this.instance.EncodeProgress -= this.InstanceEncodeProgress;
+                this.instance.Dispose();
+                this.instance = null;
+            }
+            catch (Exception exc)
+            {
+                this.ServiceLogMessage("Failed to cleanup Encode instance: " + exc );
+            }
         }
 
         private long GetFilesize(string destination)
index b11c814baba4766ba04740694b15f0bab683dc84..4cbd4c82a433032a149fa9701653d1881d483517 100644 (file)
@@ -20,9 +20,11 @@ namespace HandBrakeWPF.Services
     using HandBrake.Interop.Utilities;\r
 \r
     using HandBrakeWPF.EventArgs;\r
+    using HandBrakeWPF.Instance;\r
     using HandBrakeWPF.Properties;\r
     using HandBrakeWPF.Services.Interfaces;\r
     using HandBrakeWPF.Services.Queue.Interfaces;\r
+    using HandBrakeWPF.Services.Scan.Interfaces;\r
     using HandBrakeWPF.Utilities;\r
     using HandBrakeWPF.ViewModels.Interfaces;\r
 \r
@@ -34,20 +36,10 @@ namespace HandBrakeWPF.Services
     /// </summary>\r
     public class PrePostActionService : IPrePostActionService\r
     {\r
-        /// <summary>\r
-        /// The queue processor.\r
-        /// </summary>\r
         private readonly IQueueProcessor queueProcessor;\r
-\r
-        /// <summary>\r
-        /// The user setting service.\r
-        /// </summary>\r
         private readonly IUserSettingService userSettingService;\r
-\r
-        /// <summary>\r
-        /// The window manager.\r
-        /// </summary>\r
         private readonly IWindowManager windowManager;\r
+        private readonly IScan scanService;\r
 \r
         /// <summary>\r
         /// Initializes a new instance of the <see cref="PrePostActionService"/> class.\r
@@ -61,11 +53,12 @@ namespace HandBrakeWPF.Services
         /// <param name="windowManager">\r
         /// The window Manager.\r
         /// </param>\r
-        public PrePostActionService(IQueueProcessor queueProcessor, IUserSettingService userSettingService, IWindowManager windowManager)\r
+        public PrePostActionService(IQueueProcessor queueProcessor, IUserSettingService userSettingService, IWindowManager windowManager, IScan scanService)\r
         {\r
             this.queueProcessor = queueProcessor;\r
             this.userSettingService = userSettingService;\r
             this.windowManager = windowManager;\r
+            this.scanService = scanService;\r
 \r
             this.queueProcessor.QueueCompleted += QueueProcessorQueueCompleted;\r
             this.queueProcessor.EncodeService.EncodeCompleted += EncodeService_EncodeCompleted;\r
@@ -157,7 +150,7 @@ namespace HandBrakeWPF.Services
                     });\r
 \r
             if (!titleSpecificView.IsCancelled)\r
-            {\r
+            {               \r
                 // Do something when the encode ends.\r
                 switch (this.userSettingService.GetUserSetting<string>(UserSettingConstants.WhenCompleteAction))\r
                 {\r
@@ -166,9 +159,11 @@ namespace HandBrakeWPF.Services
                         ProcessStartInfo shutdown = new ProcessStartInfo("Shutdown", "-s -t 60");\r
                         shutdown.UseShellExecute = false;\r
                         Process.Start(shutdown);\r
+                        Execute.OnUIThread(() => System.Windows.Application.Current.Shutdown());\r
                         break;\r
                     case "Log off":\r
                     case "Ausloggen":\r
+                        this.scanService.Dispose();\r
                         Win32.ExitWindowsEx(0, 0);\r
                         break;\r
                     case "Suspend":\r
index 5458b9d2d544d706794e237339f13618b2d979da..61e34ddb411668e40f69ba709527db2f72a02d33 100644 (file)
@@ -43,7 +43,7 @@ namespace HandBrakeWPF.Services.Scan.Interfaces
     /// <summary>
     /// The IScan Interface
     /// </summary>
-    public interface IScan
+    public interface IScan : IDisposable
     {
         /// <summary>
         /// Scan has Started
index 40823f3f7b5a86607a428fc5bbb997c205ba46ed..de26ae87dbe719a3e60d0fcda75e51a8c9ed2d58 100644 (file)
@@ -15,7 +15,6 @@ namespace HandBrakeWPF.Services.Scan
     using System.Windows.Media.Imaging;
 
     using HandBrake.Interop.Interop;
-    using HandBrake.Interop.Interop.HbLib;
     using HandBrake.Interop.Interop.Interfaces;
     using HandBrake.Interop.Interop.Json.Scan;
     using HandBrake.Interop.Interop.Model;
@@ -25,26 +24,23 @@ namespace HandBrakeWPF.Services.Scan
 
     using HandBrakeWPF.Instance;
     using HandBrakeWPF.Services.Encode.Model;
-    using HandBrakeWPF.Services.Encode.Model.Models;
     using HandBrakeWPF.Services.Scan.EventArgs;
     using HandBrakeWPF.Services.Scan.Factories;
     using HandBrakeWPF.Services.Scan.Interfaces;
     using HandBrakeWPF.Services.Scan.Model;
     using HandBrakeWPF.Utilities;
 
-    using Chapter = Model.Chapter;
     using ILog = Logging.Interfaces.ILog;
     using LogLevel = Logging.Model.LogLevel;
     using LogMessageType = Logging.Model.LogMessageType;
     using LogService = Logging.LogService;
     using ScanProgressEventArgs = HandBrake.Interop.Interop.EventArgs.ScanProgressEventArgs;
-    using Subtitle = Model.Subtitle;
     using Title = Model.Title;
 
     /// <summary>
     /// Scan a Source
     /// </summary>
-    public class LibScan : IScan
+    public class LibScan : IScan, IDisposable
     {
         #region Private Variables
 
@@ -149,7 +145,16 @@ namespace HandBrakeWPF.Services.Scan
             {
                 this.ServiceLogMessage("Manually Stopping Scan ...");
                 this.IsScanning = false;
-                this.instance.StopScan();              
+              
+                var handBrakeInstance = this.instance;
+                if (handBrakeInstance != null)
+                {
+                    handBrakeInstance.StopScan();
+                    handBrakeInstance.ScanProgress -= this.InstanceScanProgress;
+                    handBrakeInstance.ScanCompleted -= this.InstanceScanCompleted;
+                    handBrakeInstance.Dispose();
+                    this.instance = null;
+                }
             }
             catch (Exception exc)
             {
@@ -158,6 +163,7 @@ namespace HandBrakeWPF.Services.Scan
             finally
             {
                 this.ScanCompleted?.Invoke(this, new ScanCompletedEventArgs(this.isCancelled, null, null, null));
+                this.instance = null;
                 this.ServiceLogMessage("Scan Stopped ...");
             }
         }
@@ -250,7 +256,7 @@ namespace HandBrakeWPF.Services.Scan
         /// The preview Count.
         /// </param>
         /// <param name="configuraiton">
-        /// The configuraiton.
+        /// The configuration.
         /// </param>
         private void ScanSource(object sourcePath, int title, int previewCount, HBConfiguration configuraiton)
         {
@@ -293,44 +299,58 @@ namespace HandBrakeWPF.Services.Scan
         /// </param>
         private void InstanceScanCompleted(object sender, System.EventArgs e)
         {
-            this.ServiceLogMessage("Processing Scan Information ...");
-            bool cancelled = this.isCancelled;
-            this.isCancelled = false;
-
-            // TODO -> Might be a better place to fix this.
-            string path = this.currentSourceScanPath;
-            if (this.currentSourceScanPath.Contains("\""))
+            try
             {
-                path = this.currentSourceScanPath.Trim('\"');
-            }
+                this.ServiceLogMessage("Processing Scan Information ...");
+                bool cancelled = this.isCancelled;
+                this.isCancelled = false;
 
-            // Process into internal structures.
-            Source sourceData = null;
-            if (this.instance?.Titles != null)
-            {
-                sourceData = new Source { Titles = ConvertTitles(this.instance.Titles), ScanPath = path };
-            }
+                // TODO -> Might be a better place to fix this.
+                string path = this.currentSourceScanPath;
+                if (this.currentSourceScanPath.Contains("\""))
+                {
+                    path = this.currentSourceScanPath.Trim('\"');
+                }
 
-            this.IsScanning = false;
+                // Process into internal structures.
+                Source sourceData = null;
+                if (this.instance?.Titles != null)
+                {
+                    sourceData = new Source { Titles = this.ConvertTitles(this.instance.Titles), ScanPath = path };
+                }
 
-            if (this.postScanOperation != null)
-            {
-                try
+                this.IsScanning = false;
+
+                if (this.postScanOperation != null)
                 {
-                    this.postScanOperation(true, sourceData);
+                    try
+                    {
+                        this.postScanOperation(true, sourceData);
+                    }
+                    catch (Exception exc)
+                    {
+                        Debug.WriteLine(exc);
+                    }
+
+                    this.postScanOperation = null; // Reset
+                    this.ServiceLogMessage("Scan Finished for Queue Edit ...");
                 }
-                catch (Exception exc)
+                else
                 {
-                    Debug.WriteLine(exc);
+                    this.ScanCompleted?.Invoke(
+                        this,
+                        new ScanCompletedEventArgs(cancelled, null, string.Empty, sourceData));
+                    this.ServiceLogMessage("Scan Finished ...");
                 }
-
-                this.postScanOperation = null; // Reset
-                this.ServiceLogMessage("Scan Finished for Queue Edit ...");
             }
-            else
+            finally
             {
-                this.ScanCompleted?.Invoke(this, new ScanCompletedEventArgs(cancelled, null, string.Empty, sourceData));
-                this.ServiceLogMessage("Scan Finished ...");
+                var handBrakeInstance = this.instance;
+                if (handBrakeInstance != null)
+                {
+                    handBrakeInstance.ScanProgress -= this.InstanceScanProgress;
+                    handBrakeInstance.ScanCompleted -= this.InstanceScanCompleted;
+                }
             }
         }
 
@@ -380,5 +400,21 @@ namespace HandBrakeWPF.Services.Scan
             return titleList;
         }
         #endregion
+
+        public void Dispose()
+        {
+            if (this.instance != null)
+            {
+                try
+                {
+                    this.instance.Dispose();
+                    this.instance = null;
+                }
+                catch (Exception e)
+                {
+                    this.ServiceLogMessage("Unable to Dispose of LibScan: " + e);
+                }            
+            }
+        }
     }
 }
\ No newline at end of file