title: Program hangs after leaving screen saver or locking computer
body: <p>Our program works fine, until someone locks the computer or the screen-saver pops up <em>(but not ctrl+alt+delete)</em>.  Once the computer is unlocked/the screen saver is closed, the application stops drawing everything except the title bar, and stops responding to input - it displays a mostly-white window which can't be moved or closed.</p>
  
  <p><img src="https://i.stack.imgur.com/Hq4LT.png" alt="Example of application freezing"></p>
  
  <p><em>(Example of application freezing - the mountains are from my desktop background)</em></p>
  
  <p>If we let it sit for about 5~10 minutes, it comes back to life, and doesn't hang again <em>(even after locking the computer/screen saver popup)</em> until the application is restarted.</p>
  
  <p>It's difficult to debug, because it doesn't happen when the program is started from Visual Studio, only when the .exe is manually opened.</p>
  
  <p><strong>It only happens when the splash-screen is shown</strong> - if I remove the code to show the splash-screen, it stops happening.  We need the splash-screen, however.</p>
  
  <p><strong>I've tried every suggestion on <a href="https://stackoverflow.com/questions/48916/multi-threaded-splash-screen-in-c">this page</a></strong>; the only one this doesn't happen with is using <code>Microsoft.VisualBasic.WindowsFormsApplicationBase</code>, but that causes <em>all sorts</em> of other problems.</p>
  
  <p>Information about this on the Internet appears to be scarce - has anyone run into a similar problem before?</p>
  
  <hr>
  
  <p>Here is the relevant code:</p>
  
  <pre><code>//Multiple programs use this login form, all have the same issue
  public partial class LoginForm&lt;TMainForm&gt;
      where TMainForm : Form, new()
  {
      private readonly Action _showLoadingForm;
  
      public LoginForm(Action showLoadingForm)
      {
          ...
          _showLoadingForm = showLoadingForm;
      }
  
      private void btnLogin_Click(object sender, EventArgs e)
      {
          ...
          this.Hide();
          ShowLoadingForm(); //Problem goes away when commenting-out this line
          new TMainForm().ShowDialog();
          this.Close();
      }
  
      private void ShowLoadingForm()
      {
          Thread loadingFormThread = new Thread(o =&gt; _showLoadingForm());
          loadingFormThread.IsBackground = true;
          loadingFormThread.SetApartmentState(ApartmentState.STA);
          loadingFormThread.Start();
      }
  }
  </code></pre>
  
  <p>Here is an example of one of the <code>_showLoadingForm</code> actions used in one of the programs:</p>
  
  <pre><code>public static bool _showSplash = true;
  public static void ShowSplashScreen()
  {
      //Ick, DoEvents!  But we were having problems with CloseSplashScreen being called
      //before ShowSplashScreen - this hack was found at
      //https://stackoverflow.com/questions/48916/multi-threaded-splash-screen-in-c/48946#48946
      using(SplashForm splashForm = new SplashForm())
      {
          splashForm.Show();
          while(_showSplash)
              Application.DoEvents();
          splashForm.Close();
      }
  }
  
  //Called in MainForm_Load()
  public static void CloseSplashScreen()
  {
      _showSplash = false;
  }
  </code></pre>