tags:

views:

202

answers:

4

Hi, I do some rather long winded things with a forms application using arrays and sometimes I address it wrongly during development, instead of an obvious error or a crash the whole application restarts and tries to run:

Application.Run(new SplashForm());

Why does this happen? It makes debugging very painful!

Thanks

A: 

Check if your project has an application level error handler that everything is bubbling up to.

You may want to put error handling code in the appropriate places in the application.

Raj More
I have error handling code already, but when it is trigged it causes the application.run() anyway
Nathan
A: 

Ok thanks for all your help but i worked it out, the problem was it was returning to the RunWorkerCompletedEvent from the background worker it was setting a label property, this works fine except that the label is named differently to the label text being set, and since im getting the control programatically and then setting the value it is hard to diagnose.

Strange behaviour though, if the control does not exist it reload the entire form, can anyone explain this?

Thanks again everyone

Nathan
+3  A: 

You might want to add error handling to your Application. Here's some code we use for that: -

namespace YourNamespace
{
    static class Program
    {

        [STAThread]
        static void Main()
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            HandleException(e.Exception);
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            HandleException((Exception)e.ExceptionObject);
        }

        static void HandleException(Exception e)
        {
     //Handle it here
        }

    }
}

Thanks,

Phil. http://exceptioneer.com

Plip
Thanks Phil, that looks great and I have added it but any errors still cause it to crash badly rather than trigger an exception, Is there a generic coverall I should add here?
Nathan
+1  A: 

It's because you're trying to invoke the UI thread on a non existing control. It's probably throwing Cross-Thread exception, and because you do not have error handling, it's crashing at the return point of the new SplashForm()

When you run the application in debug mode, check your "output" window to see if any exception messages show up. You could possibly see a message like "cross-thread operation not valid accessed from a thread other than the thread it was created on."

Michael G