App not exiting after exception?

Uniwares_ASUniwares_AS Posts: 168
edited August 3, 2010 4:40AM in SmartAssembly 5
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.
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
			}
		}
	}
}

Comments

  • Brian DonahueBrian Donahue Posts: 6,590 New member
    Hello,

    If you are using SmartAssembly's error reporting feature, there is an option to "continue" on error. Did you use that option?
  • No, not using this option.
  • Brian DonahueBrian Donahue Posts: 6,590 New member
    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.
  • Brian DonahueBrian Donahue Posts: 6,590 New member
    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.
  • Paul.MartinPaul.Martin Posts: 83 New member
    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.
  • 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).
  • Brian DonahueBrian Donahue Posts: 6,590 New member
    Magic! Thanks Paul!
Sign In or Register to comment.