Application.Exit problem

C

Chuck

Hello everybody,

I need to abort execution during start up, while the constructor called by
Application.Run is executing.

If the database fails to connect during my application's startup I want to
display a message (no problem here) and then abort the program. However,
after the attached code executes I end up with my main form and a wait
cursor! If I click on the X the form closes. Boss doesn't think an
unhandled exception will impress our customers :) Can't say that I
disagree.

Anybody have any ideas what I am doing wrong? (Besides, programming for a
living).

public MainFrame() //MDI appication
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
try
{
Initilize(); //attempts to connect to remote database. throws if it
can't.
}
catch(System.Net.WebException we) // this is thrown when the remote
database can't be found.
{
string message = we.Message; //message explaining that database can't
be found. (my text);
message += "\nNotify System Administrator.\nClosing application";
//some add-on text.
MessageBox.Show(message,"Database
Error!",MessageBoxButtons.OK,MessageBoxIcon.Error);
connectFailed = true; //needed to prevent OnLoad event handler from
doing its thing. Form loads even though I have called Exit!
Application.ExitThread(); //This doesn't seem to work. Neither does
Application.Exit() or this.Close();
}
catch(Exception e)
{
throw e; // just a place to put a break point for debugging.
}
}

[STAThread]
static void Main()
{
Application.EnableVisualStyles(); // I am running XP pro.
//Get the running instance.
Process instance = RunningInstance(); //checks for a running instance
of the application. Uses win32 API
if (instance == null)
{
//There isn't another instance, show our form.
SplashScreen.ShowSplashScreen(); // runs on separate thread.
Application.DoEvents();
Application.Run(new MainFrame());
}
else
{
//There is another instance of this process.
HandleRunningInstance(instance);
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

Chuck,

First, I think that the way that you are checking for a running instance
isn't the best. You can easily use a named mutex (use the Mutex class from
the System.Threading namespace) to keep multiple instances of your
application from running. I like to use the full name of the entry point
type as the mutex name (it's pretty unique).

As for your issue with the exception, I think that you should not try to
exit the application during the constructor of the form that will handle the
main windows message processing. You should establish your database
connection outside of the form constructor (before the Run method is
called), and then pass the connection to the Form if it connects
sucessfully. Otherwise, don't call Run at all.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Chuck said:
Hello everybody,

I need to abort execution during start up, while the constructor called by
Application.Run is executing.

If the database fails to connect during my application's startup I want to
display a message (no problem here) and then abort the program. However,
after the attached code executes I end up with my main form and a wait
cursor! If I click on the X the form closes. Boss doesn't think an
unhandled exception will impress our customers :) Can't say that I
disagree.

Anybody have any ideas what I am doing wrong? (Besides, programming for a
living).

public MainFrame() //MDI appication
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
try
{
Initilize(); //attempts to connect to remote database. throws if it
can't.
}
catch(System.Net.WebException we) // this is thrown when the remote
database can't be found.
{
string message = we.Message; //message explaining that database can't
be found. (my text);
message += "\nNotify System Administrator.\nClosing application";
//some add-on text.
MessageBox.Show(message,"Database
Error!",MessageBoxButtons.OK,MessageBoxIcon.Error);
connectFailed = true; //needed to prevent OnLoad event handler from
doing its thing. Form loads even though I have called Exit!
Application.ExitThread(); //This doesn't seem to work. Neither does
Application.Exit() or this.Close();
}
catch(Exception e)
{
throw e; // just a place to put a break point for debugging.
}
}

[STAThread]
static void Main()
{
Application.EnableVisualStyles(); // I am running XP pro.
//Get the running instance.
Process instance = RunningInstance(); //checks for a running instance
of the application. Uses win32 API
if (instance == null)
{
//There isn't another instance, show our form.
SplashScreen.ShowSplashScreen(); // runs on separate thread.
Application.DoEvents();
Application.Run(new MainFrame());
}
else
{
//There is another instance of this process.
HandleRunningInstance(instance);
}
}
 
C

Chuck

Thanks, Nicholas, for your advice.

I will check out the use of the Mutex method to prevent multiple instances.
The way I am doing it is too slow.

The idea of running my connection code before I run Application.Run() sounds
interesting. I will, also, look into doing that.

I did, however, find a way to make things work. I moved the
Application.ExitThread() call to MainFrame_Load, the handler for the Load
event. I then used the Boolean switch that I set in the catch block to
determine if I wanted to do normal load things or abort. It works!

Chuck.
Nicholas Paldino said:
Chuck,

First, I think that the way that you are checking for a running instance
isn't the best. You can easily use a named mutex (use the Mutex class from
the System.Threading namespace) to keep multiple instances of your
application from running. I like to use the full name of the entry point
type as the mutex name (it's pretty unique).

As for your issue with the exception, I think that you should not try to
exit the application during the constructor of the form that will handle the
main windows message processing. You should establish your database
connection outside of the form constructor (before the Run method is
called), and then pass the connection to the Form if it connects
sucessfully. Otherwise, don't call Run at all.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Chuck said:
Hello everybody,

I need to abort execution during start up, while the constructor called by
Application.Run is executing.

If the database fails to connect during my application's startup I want to
display a message (no problem here) and then abort the program. However ,
after the attached code executes I end up with my main form and a wait
cursor! If I click on the X the form closes. Boss doesn't think an
unhandled exception will impress our customers :) Can't say that I
disagree.

Anybody have any ideas what I am doing wrong? (Besides, programming for a
living).

public MainFrame() //MDI appication
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
try
{
Initilize(); //attempts to connect to remote database. throws if it
can't.
}
catch(System.Net.WebException we) // this is thrown when the remote
database can't be found.
{
string message = we.Message; //message explaining that database can't
be found. (my text);
message += "\nNotify System Administrator.\nClosing application";
//some add-on text.
MessageBox.Show(message,"Database
Error!",MessageBoxButtons.OK,MessageBoxIcon.Error);
connectFailed = true; //needed to prevent OnLoad event handler from
doing its thing. Form loads even though I have called Exit!
Application.ExitThread(); //This doesn't seem to work. Neither does
Application.Exit() or this.Close();
}
catch(Exception e)
{
throw e; // just a place to put a break point for debugging.
}
}

[STAThread]
static void Main()
{
Application.EnableVisualStyles(); // I am running XP pro.
//Get the running instance.
Process instance = RunningInstance(); //checks for a running instance
of the application. Uses win32 API
if (instance == null)
{
//There isn't another instance, show our form.
SplashScreen.ShowSplashScreen(); // runs on separate thread.
Application.DoEvents();
Application.Run(new MainFrame());
}
else
{
//There is another instance of this process.
HandleRunningInstance(instance);
}
}
 
J

Jon Skeet [C# MVP]

Chuck said:
I need to abort execution during start up, while the constructor called by
Application.Run is executing.

If the database fails to connect during my application's startup I want to
display a message (no problem here) and then abort the program. However,
after the attached code executes I end up with my main form and a wait
cursor! If I click on the X the form closes. Boss doesn't think an
unhandled exception will impress our customers :) Can't say that I
disagree.

Well, three options:

1) Don't do the connection within the form construction. Do it before
you construct the form, but while the splash screen is up.

2) Handle the exception so that customers don't see an unhandled
exception. Don't forget that until your constructor has returned,
Application.Run hasn't been called, which is why Application.Exit isn't
working. A simple try/catch in your Main method should work here, I
believe.

3) Use Environment.Exit instead.
 
N

Nicholas Paldino [.NET/C# MVP]

Chuck,

I imagine you are cycling through all the process names and determining
if one exists already (or something equally painful).

Using the mutex, you basically are blocking access to other mutexes with
the same name. Your code, instead of running then checking all other
processes to see if there is a similar one, would just have to check if a
well known, unique name exists. The code goes something like this:

public class EntryPoint
{
public static void Main()
{
// Was the mutex acquired?
bool pblnAcquired = false;

// Try and acquire the mutex. Use the full type name as the mutex
name.
using (Mutex pobjMutex = new Mutex(true,
typeof(EntryPoint).FullName, ref pblnAcquired)
{
// If the mutex was not acquired, then get out.
if (!pblnAcquired)
// Get out.
return;

// Run the application loop here.
Application.Run(new MainForm());
}

// Get out.
return;
}


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Chuck said:
Thanks, Nicholas, for your advice.

I will check out the use of the Mutex method to prevent multiple instances.
The way I am doing it is too slow.

The idea of running my connection code before I run Application.Run() sounds
interesting. I will, also, look into doing that.

I did, however, find a way to make things work. I moved the
Application.ExitThread() call to MainFrame_Load, the handler for the Load
event. I then used the Boolean switch that I set in the catch block to
determine if I wanted to do normal load things or abort. It works!

Chuck.
message news:%[email protected]...
Chuck,

First, I think that the way that you are checking for a running instance
isn't the best. You can easily use a named mutex (use the Mutex class from
the System.Threading namespace) to keep multiple instances of your
application from running. I like to use the full name of the entry point
type as the mutex name (it's pretty unique).

As for your issue with the exception, I think that you should not
try
to
exit the application during the constructor of the form that will handle the
main windows message processing. You should establish your database
connection outside of the form constructor (before the Run method is
called), and then pass the connection to the Form if it connects
sucessfully. Otherwise, don't call Run at all.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Chuck said:
Hello everybody,

I need to abort execution during start up, while the constructor
called
want
However
for
a
living).

public MainFrame() //MDI appication
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
try
{
Initilize(); //attempts to connect to remote database. throws if it
can't.
}
catch(System.Net.WebException we) // this is thrown when the remote
database can't be found.
{
string message = we.Message; //message explaining that database can't
be found. (my text);
message += "\nNotify System Administrator.\nClosing application";
//some add-on text.
MessageBox.Show(message,"Database
Error!",MessageBoxButtons.OK,MessageBoxIcon.Error);
connectFailed = true; //needed to prevent OnLoad event handler from
doing its thing. Form loads even though I have called Exit!
Application.ExitThread(); //This doesn't seem to work. Neither does
Application.Exit() or this.Close();
}
catch(Exception e)
{
throw e; // just a place to put a break point for debugging.
}
}

[STAThread]
static void Main()
{
Application.EnableVisualStyles(); // I am running XP pro.
//Get the running instance.
Process instance = RunningInstance(); //checks for a running instance
of the application. Uses win32 API
if (instance == null)
{
//There isn't another instance, show our form.
SplashScreen.ShowSplashScreen(); // runs on separate thread.
Application.DoEvents();
Application.Run(new MainFrame());
}
else
{
//There is another instance of this process.
HandleRunningInstance(instance);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top