title: C# WPF how to enforce single instances of windows
body: <p>I'd like to know what's the best way (read most elegant) to have a single instance of a given Window per application in WPF.</p>
  
  <p>I'm a newcomer to .NET and WPF and what I came up with looks pretty lame.</p>
  
  <pre><code>private static readonly Object MUTEX = new Object();
  private static AboutWindow INSTANCE;
  
  public static AboutWindow GetOrCreate() {
      lock (MUTEX) {
          if (INSTANCE == null) {
              INSTANCE = new AboutWindow();
          }
          INSTANCE.Show();
          return INSTANCE;
      }
  }
  
  private AboutWindow() {
      InitializeComponent();
  }
  
  private void AboutWindow_Closed(object sender, EventArgs e) {
      // the Closed events are handy for me to update values across
      // different windows.
      lock (MUTEX) {
          INSTANCE = null;
      }
  }
  </code></pre>
  
  <p>Thing is... this looks like utter crap. There must be some way to achieve the same goal in a much more elegant way, right?</p>
  
  <p>PS: I'm often using the <code>Closed</code> event to change values in other open windows. For instance I have the <strong>SettingsWindow</strong> with the "Account" button. When I push that button, the <strong>AccountWindow</strong> pops up. When I close <strong>AcountWindow</strong>, I want something in the <strong>SettingsWindow</strong> to change (a label). Hence the constant creation of windows.<br>
  Besides, Close is something you always have to deal with because of the X button on the window frame...</p>