When my C# form crashes it tries to create a new instance of itself
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 pain开发者_如何学JAVAful!
Thanks
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
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."
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.
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
精彩评论