Browse Source

Improved dispose method

Bond-009 6 years ago
parent
commit
d623f616fa
1 changed files with 22 additions and 10 deletions
  1. 22 10
      Emby.Dlna/PlayTo/PlayToController.cs

+ 22 - 10
Emby.Dlna/PlayTo/PlayToController.cs

@@ -607,22 +607,34 @@ namespace Emby.Dlna.PlayTo
 
         public void Dispose()
         {
-            if (!_disposed)
-            {
-                _disposed = true;
+            Dispose(true);
+            GC.SuppressFinalize(this);
+        }
 
-                _device.PlaybackStart -= _device_PlaybackStart;
-                _device.PlaybackProgress -= _device_PlaybackProgress;
-                _device.PlaybackStopped -= _device_PlaybackStopped;
-                _device.MediaChanged -= _device_MediaChanged;
-                //_deviceDiscovery.DeviceLeft -= _deviceDiscovery_DeviceLeft;
-                _device.OnDeviceUnavailable = null;
+        protected virtual void Dispose(bool disposing)
+        {
+            if (_disposed)
+            {
+                return;
+            }
 
+            if (disposing)
+            {
                 _device.Dispose();
             }
+
+            _device.PlaybackStart -= _device_PlaybackStart;
+            _device.PlaybackProgress -= _device_PlaybackProgress;
+            _device.PlaybackStopped -= _device_PlaybackStopped;
+            _device.MediaChanged -= _device_MediaChanged;
+            _deviceDiscovery.DeviceLeft -= _deviceDiscovery_DeviceLeft;
+            _device.OnDeviceUnavailable = null;
+            _device = null;
+
+            _disposed = true;
         }
 
-        private readonly CultureInfo _usCulture = new CultureInfo("en-US");
+        private static readonly CultureInfo _usCulture = CultureInfo.ReadOnly(new CultureInfo("en-US"));
 
         private Task SendGeneralCommand(GeneralCommand command, CancellationToken cancellationToken)
         {