I have written a bit of code to check driver versions and would like to have it installed and run first from an installer. Once the code has executed it should exit as
MessageBox.Show("Version of driver is " + Version_Number, "", MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.Exit();
It displays a messagebox and should end with Application.Exit() except it does not. it loads the form and waits for a button click at which it displays the message box again and then exits.
My question is how to get the program to end after the message box has been shown? (ideally with the form invisible).
Glenn

VulpesRank #55 · 29.2K reputationPosted Jul 9, 2012, 8:02 AM EDT
If you're on the main form, then: this.Close(); should suffice to close the main form and the application as a whole.
Also Application.Exit() will cause the FormClosing/FormClosed events to be called for each open form. So, if you have any code which handles these events, it will be executed before the application closes.
If you can't figure out what the problem is, then Environment.Exit(1) should kill the application there and then though I'd only use it as a last resort.
Glenn PattonRank #6,159 · 275 reputationPosted Jul 9, 2012, 8:13 AM EDT
I had thanks to google come across Enviroment.Exit(0); and was wondering if there was a penalty for doing this it shall be investigated tomorrow!
Thanks
Glenn
VulpesRank #55 · 29.2K reputationPosted Jul 9, 2012, 8:25 AM EDT
It causes an abrupt exit whereas Application.Exit() causes a more orderly exit.
Glenn PattonRank #6,159 · 275 reputationPosted Jul 10, 2012, 12:43 AM EDT
Tried Application.Exit() as said was tried and didn't work Enviroment.Exit(0) seems to do the job nicely. Using this.Close() causes a ObjectDisposedException was unhandled error/warning.
If I use Enviroment.Exit(0) I was wondering if it left anything hanging around (such as non-terminated threads) that could come and bite later as it seems to work.
Glenn
VulpesRank #55 · 29.2K reputationPosted Jul 10, 2012, 12:59 AM EDT
However, if you have any clean up to do before closing the application such as saving stuff to files, then you need to do this first before calling it.
One thing that can prevent Application.Exit from working is if you've created your own threads (as opposed to using a BackgroundWorker or the ThreadPool) and have omitted to set the IsBackground property to true for those threads. This property is false by default.
The problem here is that an application cannot close normally until all foreground threads have ended so you might have a situation where the UI thread has ended but one or more of your other threads are still running and hence preventing the application from closing. Worse still, the UI may have disappeared and so you might not even know that this is happening without checking Task Manager!
Glenn PattonRank #6,159 · 275 reputationPosted Jul 10, 2012, 1:11 AM EDT
Glenn
VulpesRank #55 · 29.2K reputationPosted Jul 10, 2012, 1:50 AM EDT
The exception suggests that this is triggering another call to this.Close, this.Dispose or Application.Exit.
That might happen, for instance, if you're handling the FormClosing or FormClosed events and have a call to one of those methods in the handler.