473,388 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Stupid Application.Exit problem....

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
Nov 16 '05 #1
3 5063
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]
Nov 16 '05 #2
ZoombyWoof, try to close db connection, and then call
Process.GetCurrentProcess.Kill()
--
Vladimir Scherbina
Ukraine, Kiev.
--

"ZoombyWoof" <zo**************@thishotmail.com> wrote in message
news:pa****************************@thishotmail.co m...
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

Nov 16 '05 #3
On Wed, 05 May 2004 12:53:34 +0200, Morten Wennevik wrote:
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

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Brendan Miller | last post by:
I am trying to close my application using Application.exit() in the frmMain_Closing event. When the form closes the process does not. My application only has one form (no other classes either). ...
4
by: Chuck | last post by:
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...
6
by: orekin | last post by:
Hi There I have been trying to come to grips with Application.Run(), Application.Exit() and the Message Pump and I would really appreciate some feedback on the following questions .. There are...
3
by: Mike Johnson | last post by:
I have the following code in form1 and when the application starts this sub is called to check for a path if not found a message box is displayed and then gives the user an option to end the...
2
by: Mike Johnson | last post by:
The sub is being called from the Sub New(). If I can't use Application.Exit() in this situation then how do I exit the application? please help. Public Sub Check_For_Dir() Dim MyPath, MyName As...
3
by: marciocamurati | last post by:
Hi, I have a problem with my application exit, I create a button that call another form and close my application: Private Sub closeApplication() Dim Status As New Status status.Show() ...
4
by: vul | last post by:
I start the application with Application.Run(New MDIMain) in Sub Main. MDIMain is the mdi form which loads and then calls Login form. I'm using Application.Exit to terminate my VB 2005 application...
4
by: JIM.H. | last post by:
Here is the code I am having problem: static void Main(string args) { bool isPar = false; if ((args.Length == 1)) { if ((args(0).ToUpper() == "MYPAR")) { isPar = true;
1
by: Chris Cairns | last post by:
I have a MDI Application and would like to prompt the user before exit. I placed the following in the FormClosing event. It appears to work properly, however when a user answers no to the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.