title: WPF Single Instance Best Practices
body: <p>This is the code I implemented so far to create a single instance WPF application:</p>
  
  <pre><code>#region Using Directives
  using System;
  using System.Globalization;
  using System.Reflection;
  using System.Threading;
  using System.Windows;
  using System.Windows.Interop;
  #endregion
  
  namespace MyWPF
  {
      public partial class MainApplication : Application, IDisposable
      {
          #region Members
          private Int32 m_Message;
          private Mutex m_Mutex;
          #endregion
  
          #region Methods: Functions
          private IntPtr HandleMessages(IntPtr handle, Int32 message, IntPtr wParameter, IntPtr lParameter, ref Boolean handled)
          {
              if (message == m_Message)
              {
                  if (MainWindow.WindowState == WindowState.Minimized)
                      MainWindow.WindowState = WindowState.Normal;
  
                  Boolean topmost = MainWindow.Topmost;
  
                  MainWindow.Topmost = true;
                  MainWindow.Topmost = topmost;
              }
  
              return IntPtr.Zero;
          }
  
          private void Dispose(Boolean disposing)
          {
              if (disposing &amp;&amp; (m_Mutex != null))
              {
                  m_Mutex.ReleaseMutex();
                  m_Mutex.Close();
                  m_Mutex = null;
              }
          }
  
          public void Dispose()
          {
              Dispose(true);
              GC.SuppressFinalize(this);
          }
          #endregion
  
          #region Methods: Overrides
          protected override void OnStartup(StartupEventArgs e)
          {
              Assembly assembly = Assembly.GetExecutingAssembly();
              Boolean mutexCreated;
              String mutexName = String.Format(CultureInfo.InvariantCulture, "Local\\{{{0}}}{{{1}}}", assembly.GetType().GUID, assembly.GetName().Name);
  
              m_Mutex = new Mutex(true, mutexName, out mutexCreated);
              m_Message = NativeMethods.RegisterWindowMessage(mutexName);
  
              if (!mutexCreated)
              {
                  m_Mutex = null;
  
                  NativeMethods.PostMessage(NativeMethods.HWND_BROADCAST, m_Message, IntPtr.Zero, IntPtr.Zero);
  
                  Current.Shutdown();
  
                  return;
              }
  
              base.OnStartup(e);
  
              MainWindow window = new MainWindow();
              MainWindow = window;
              window.Show(); 
  
              HwndSource.FromHwnd((new WindowInteropHelper(window)).Handle).AddHook(new HwndSourceHook(HandleMessages));
          }
  
          protected override void OnExit(ExitEventArgs e)
          {
              Dispose();
              base.OnExit(e);
          }
          #endregion
      }
  }
  </code></pre>
  
  <p>Everything works perfectly... but I have some doubts about it and I would like to receive your suggestions about how my approach could be improved.</p>
  
  <p>1) I was asked by Code Analysis to implement <code>IDisposable</code> interface because I was using <code>IDisposable</code> members (the <code>Mutex</code>). Is my <code>Dispose()</code> implementation good enough? Should I avoid it because it's never going to be called?</p>
  
  <p>2) It's better to use <code>m_Mutex = new Mutex(true, mutexName, out mutexCreated);</code> and check for the result or to use <code>m_Mutex = new Mutex(false, mutexName);</code> and then check for <code>m_Mutex.WaitOne(TimeSpan.Zero, false);</code> ? In case of multithreading I mean... </p>
  
  <p>3) <code>RegisterWindowMessage</code> API call should return <code>UInt32</code>... but <code>HwndSourceHook</code> is only accepting <code>Int32</code> as message value... should I be worried about unexpected behaviors (like a result bigger than <code>Int32.MaxValue</code>)?</p>
  
  <p>4) In <code>OnStartup</code> override... should I execute <code>base.OnStartup(e);</code> even if another instance is already running and I'm going to shutdown the application?</p>
  
  <p>5) Is there a better way to bring the existing instance to the top that doesn't need to set <code>Topmost</code> value? Maybe <code>Activate()</code>?</p>
  
  <p>6) Can you see any flaw in my approach? Something concerning multithreading, bad exceptions handling and something like that? For example... what happens if my application crashes between <code>OnStartup</code> and <code>OnExit</code>?</p>