Stupid Application.Exit problem....

Z

ZoombyWoof

Hi.
I'm pretty new to C# so this may seem like the dumbest question
today, but I'm stuck.
I have a small application, one Form that opens a connection to
a database in its constructor. If the connection goes wrong I
display a messagebox and I want the application to end after the user
clicks OK.
I cant make that happen.....
If I use Application.Exit in my constructor the form still lives,
if I do a this.dispose the program crashes and the debugger throws up on
me, pointing to the row after Application.Run(blabla) in main.
Application.ExitThread doesnt work either.

How can I kill my program from the Form:s constructor ?

Any help would be much appreciated. Thanx

/ZW
 
M

Morten Wennevik

Hi ZoombyWoof,

You can put the connection code in your Main method or another static
method and if it fails, display a message and return without using
Application.Run();

public static void Main()
{
if(//some code fails)
{
// show error message
}
Application.Run(new MainForm());
}

Either use a static connection object, or pass the object in the
constructor of MainForm


Happy coding!
Morten Wennevik [C# MVP]
 
V

Vladimir Scherbina

ZoombyWoof, try to close db connection, and then call
Process.GetCurrentProcess.Kill()
 
Z

ZoombyWoof

Hi ZoombyWoof,

You can put the connection code in your Main method or another static
method and if it fails, display a message and return without using
Application.Run();

public static void Main()
{
if(//some code fails)
{
// show error message
}
Application.Run(new MainForm());
}
}
Either use a static connection object, or pass the object in the
constructor of MainForm


Happy coding!
Morten Wennevik [C# MVP]
Thanx! I didn't think of putting any other code in main than
Application.Run :)
It works just as I want it now.

/ZW
 

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