From 57dd61d2090bf987e6f57b87a22779707361d7d3 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 23 Apr 2013 10:53:43 -0400 Subject: [PATCH] fixes #166 - Multiple instance crash. --- MediaBrowser.ServerApplication/App.xaml.cs | 44 ++++++++++++---------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/MediaBrowser.ServerApplication/App.xaml.cs b/MediaBrowser.ServerApplication/App.xaml.cs index f446b4dc4..3c1524eec 100644 --- a/MediaBrowser.ServerApplication/App.xaml.cs +++ b/MediaBrowser.ServerApplication/App.xaml.cs @@ -24,12 +24,27 @@ namespace MediaBrowser.ServerApplication /// public partial class App : Application { + /// + /// The single instance mutex + /// + private static Mutex _singleInstanceMutex; + /// /// Defines the entry point of the application. /// [STAThread] public static void Main() { + bool createdNew; + + _singleInstanceMutex = new Mutex(true, @"Local\" + typeof(App).Assembly.GetName().Name, out createdNew); + + if (!createdNew) + { + _singleInstanceMutex = null; + return; + } + // Look for the existence of an update archive var appPaths = new ServerApplicationPaths(); var updateArchive = Path.Combine(appPaths.TempUpdatePath, Constants.MbServerPkgName + ".zip"); @@ -66,11 +81,6 @@ namespace MediaBrowser.ServerApplication } } - /// - /// The single instance mutex - /// - private Mutex SingleInstanceMutex; - /// /// Gets or sets the logger. /// @@ -107,15 +117,6 @@ namespace MediaBrowser.ServerApplication /// A that contains the event data. protected override void OnStartup(StartupEventArgs e) { - bool createdNew; - SingleInstanceMutex = new Mutex(true, @"Local\" + GetType().Assembly.GetName().Name, out createdNew); - if (!createdNew) - { - SingleInstanceMutex = null; - Shutdown(); - return; - } - AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; LoadKernel(); @@ -190,7 +191,10 @@ namespace MediaBrowser.ServerApplication base.OnExit(e); - CompositionRoot.Dispose(); + if (CompositionRoot != null) + { + CompositionRoot.Dispose(); + } } /// @@ -198,15 +202,15 @@ namespace MediaBrowser.ServerApplication /// private void ReleaseMutex() { - if (SingleInstanceMutex == null) + if (_singleInstanceMutex == null) { return; } - SingleInstanceMutex.ReleaseMutex(); - SingleInstanceMutex.Close(); - SingleInstanceMutex.Dispose(); - SingleInstanceMutex = null; + _singleInstanceMutex.ReleaseMutex(); + _singleInstanceMutex.Close(); + _singleInstanceMutex.Dispose(); + _singleInstanceMutex = null; } ///