473,625 Members | 3,329 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stupid Application.Exi t 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.Exi t 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.Exi tThread 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 5084
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.GetCurr entProcess.Kill ()
--
Vladimir Scherbina
Ukraine, Kiev.
--

"ZoombyWoof " <zo************ **@thishotmail. com> wrote in message
news:pa******** *************** *****@thishotma il.com...
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.Exi t 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.Exi tThread 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
17686
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). The form has a really long loop which generates approx. 700 .csv files. I do not create any threads myself (Application.exitthread() doesn't work either). To counteract this I have decided to use the Environment.exit(-1) method instead. What...
4
11153
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 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...
6
20065
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 quite a few words in this post but the questions are actually quite similar and should be fairly quick to answer ... (1) What is Happening with the Threads
3
1543
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 application but when this option is selected the application does not end but continues to the next line of code which tries to setup a filewatcher with the path which does not exist, which then gives an error. please help. Public Sub Check_For_Dir()...
2
1357
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 String MyPath = FilePath ' Set the path. MyName = Dir(MyPath, FileAttribute.Directory) ' Retrieve the first entry. For i As Integer = 0 To 9999
3
1670
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() Application.Exit()
4
4109
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 on Login screen if user clicks on Cancel button. When I do this I get a sound as MessageBox produces. I do not see any messages, but VB environment works abnormally after that. It doesn't switch modules until I minimize it and maximize again or...
4
9504
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
7037
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 question it causes the application to flash and deactivate. I was able to activate it again but it looks sort of stupid to have the application flash when it should simply return the user to the app. Any ideas around this? I know I can do it easliy...
0
8256
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8189
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8694
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8356
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6118
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4193
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.