<p>This question is slightly related to <a href="https://stackoverflow.com/questions/61354/how-to-get-entire-chain-of-exceptions-in-applicationthreadexception-event-handl">this question about exception handling</a>. The workaround I found there consists of rolling my own message loop.</p>

<p>So my Main method now looks basically like this:</p>

<pre><code>[STAThread]
static void Main() {
  // this is needed so there'll actually an exception be thrown by
  // Application.Run/Application.DoEvents, instead of the ThreadException
  // event being raised.
  Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);

  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);

  Form form = new MainForm();
  form.Show();

  // the loop is here to keep app running if non-fatal exception is caught.
  do {
    try {
      Application.DoEvents();
      Thread.Sleep(100);
    }
    catch (Exception ex) {
      ExceptionHandler.ConsumeException(ex);
    }
  }
  while (!form.IsDisposed);
}
</code></pre>

<p>What I'm wondering though, <strong>is this a safe/decent way to replace the more typical 
'Application.Run(new MainForm());'</strong>, whether it's used for exception handling or for whatever else, or should I always stick to using Application.Run?</p>

<p>On another app that's in testing now a similar approach is used for both loading (splashscreen) and exception handling, and I don't think it has caused any troubles (yet :-))</p>

[{'answer_id': 61393, 'author': 'ima', 'author_id': 5733, 'author_profile': 'https://Stackoverflow.com/users/5733', 'pm_score': 2, 'selected': False, 'text': '<p>Pitfall 1: </p>\n\n<pre><code>Thread.Sleep(100);\n</code></pre>\n\n<p>Never. Use WaitMessage().</p>\n\n<p>Otherwise, it is possible roll out your own message loop, but in your scenario it seems somewhat pointless.</p>\n\n<p>You may also want to examine Application.Run() code (with .Net Reflector, for instance).</p>\n'}, {'answer_id': 61420, 'author': 'Quibblesome', 'author_id': 1143, 'author_profile': 'https://Stackoverflow.com/users/1143', 'pm_score': 0, 'selected': False, 'text': '<p>Yes... I think some components wont work with that code. Some of them require to live in a thread that has an Application.Run in it to effectively pick up their messages.</p>\n'}, {'answer_id': 63590, 'author': 'Mike Dimmick', 'author_id': 6970, 'author_profile': 'https://Stackoverflow.com/users/6970', 'pm_score': 2, 'selected': False, 'text': '<p>If you want to customize message processing, consider implementing IMessageFilter, then call Application.AddMessageFilter to tell the standard message pump to call your filter function.</p>\n'}]
['https://Stackoverflow.com/questions/61366', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5422/']