Red Gate forums :: View topic - App not exiting after exception?
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
SmartAssembly 5
SmartAssembly 5 forum

App not exiting after exception?

Search in SmartAssembly 5 forum
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.
Jump to:  
Author Message
Uniwares_AS



Joined: 11 Oct 2007
Posts: 132

PostPosted: Tue Jul 27, 2010 1:50 am    Post subject: App not exiting after exception? Reply with quote

Something really strange seems to happen in this little commandline app: when the exception is thrown, the app does not terminate when processed with SA.

This is just a really simple app which should just do its job while providing feedback in the background on a separate thread. No big design, just a hack. I reduced the code to the minimum to repro the case.

Without SA the app exits as expected when the exception occurs. With SA it seems to terminate only the main thread.

Code:
namespace test
{
   public class Program
   {
      private Thread dummy = new Thread(new ThreadStart(DummyDisplay));
      
      public static void Main(string[] args)
      {
         Program me = new Program();
         me.dummy.Start();
         throw new ArgumentException();

// continue program here
      }

      private static void DummyDisplay()
      {
         while(true)
         {
// do some processing, and then sleep for a long time
         }
      }
   }
}

Back to top
View user's profile Send private message MSN Messenger
Brian Donahue



Joined: 23 Aug 2004
Posts: 6581

PostPosted: Wed Jul 28, 2010 9:12 am    Post subject: Reply with quote

Hello,

If you are using SmartAssembly's error reporting feature, there is an option to "continue" on error. Did you use that option?
Back to top
View user's profile Send private message
Uniwares_AS



Joined: 11 Oct 2007
Posts: 132

PostPosted: Wed Jul 28, 2010 1:36 pm    Post subject: Reply with quote

No, not using this option.
Back to top
View user's profile Send private message MSN Messenger
Brian Donahue



Joined: 23 Aug 2004
Posts: 6581

PostPosted: Fri Jul 30, 2010 11:32 am    Post subject: Reply with quote

It must be something to do with the error reporting. .NET Runtime's default behavior is to exit the process on an unhandled exception and SA must be changing the code so that the exception is "handled" as far as .NET is concerned. If you think about it, it would be impossible for SA's exception handling to work if it allowed the runtime to terminate the whole process when an exception is thrown. But you would think that the SA exception handling code would exit the process after displaying the dialog.
Back to top
View user's profile Send private message
Brian Donahue



Joined: 23 Aug 2004
Posts: 6581

PostPosted: Sat Jul 31, 2010 10:54 am    Post subject: Reply with quote

I think my theory has some validity. SmartAssembly error reporting implements a ThreadExceptionEventHandler, which overrides what the runtime would do (kill the process).

I'll have a look at the code on Monday try to work out what it's actually doing.
Back to top
View user's profile Send private message
Paul.Martin



Joined: 03 Feb 2010
Posts: 83
Location: Cambridgeshire

PostPosted: Mon Aug 02, 2010 10:14 pm    Post subject: Reply with quote

I think the problems is that thread you are creating is a foreground thread.

There are two types of threads in .NET; background threads will automatically get terminated when an application ends, whereas all foreground threads must exit before the application will exit.

Without SA the exception will reach to the JIT debugger which is the only thing that can end all of a processes threads instantly.
SA has to add the code to handle the exception otherwise it would fall to the JIT debugger and the user would get two error messages. The most that SA can do is tell the application to exit in every way possible. It could add some really nasty hacky code which went through and terminated all of the threads in the current process, but this wouldn't handle all of the tidying up (e.g. of unmanaged resources) of the other threads.

Only thing I can really suggest is to make the thread a background thread (thread.IsBackground = true), as usually you don't need a foreground thread.
Back to top
View user's profile Send private message
Uniwares_AS



Joined: 11 Oct 2007
Posts: 132

PostPosted: Mon Aug 02, 2010 10:30 pm    Post subject: Reply with quote

True, i wont need a foreground thread. Still its a nasty trap to fall into, even if its not SA's fault. One of those cases where its hard to draw the line. But might be worth at least a warning in the user manual (or even at the settings page).
Back to top
View user's profile Send private message MSN Messenger
Brian Donahue



Joined: 23 Aug 2004
Posts: 6581

PostPosted: Tue Aug 03, 2010 9:40 am    Post subject: Reply with quote

Magic! Thanks Paul!
Back to top
View user's profile Send private message
Display posts from previous:   
This topic is locked: you cannot edit posts or make replies. All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group