tags:

views:

636

answers:

3

This question is slightly related to this question about exception handling. The workaround I found there consists of rolling my own message loop.

So my Main method now looks basically like this:

[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);
}

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

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 :-))

+2  A: 

Pitfall 1:

Thread.Sleep(100);

Never. Use WaitMessage().

Otherwise, it is possible roll out your own message loop, but in your scenario it seems somewhat pointless.

You may also want to examine Application.Run() code (with .Net Reflector, for instance).

ima
There doesn't seem to be a WaitMessage equivalent in .NET, and using P/Invoke just for a message loop seems a bit involving. (not that it's hard, but it can't be ment that way..)Also I don't have another solution for my other question (see link in this question), so why is it pointless? :p
Tobi
Pardon me, but if you think pinvoke is too involving you should think again about trying to write custom message loop. Regarding your other question, if you for some obscure reason want to handle exceptions in _unhandled_ exception event, be prepared for some hacks.
ima
In my experience decent highlevel exception handling is always one big hack and I want to get it done as well as possible. Getting just the innermost exception in debug/log output is NOT an option, and copy pasting try catch blocks in all event handlers isn't either...
Tobi
With regards to WaitMessage, I just ain't gonna do P/Invoke until Thread.Sleep actually gives some serious trouble...http://www.gamedev.net/community/forums/topic.asp?topic_id=254151
Tobi
Wrt handling exceptions in the Application.ThreadException event (which isn't really a unhandled exception event in the win32 notion, AFAIK): that's actually what I want to prevent. But it requires me to roll my own messageloop, or sprinkle try catch blocks throughout the code...
Tobi
1. "Highlevel" exception handling is bad and highly discouraged practice.2. Plain wrong. Those people have no idea what they are talking about.3. ThreadException is there to display message about unhandled exception and close application.
ima
"... I just ain't gonna do P/Invoke ..." Good luck, that's an attitude of a real pro.
ima
A: 

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.

Quibblesome
+2  A: 

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.

Mike Dimmick