473,721 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Application.Exi t problem

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
//
InitializeCompo nent();
try
{
Initilize(); //attempts to connect to remote database. throws if it
can't.
}
catch(System.Ne t.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,"Datab ase
Error!",Message BoxButtons.OK,M essageBoxIcon.E rror);
connectFailed = true; //needed to prevent OnLoad event handler from
doing its thing. Form loads even though I have called Exit!
Application.Exi tThread(); //This doesn't seem to work. Neither does
Application.Exi t() or this.Close();
}
catch(Exception e)
{
throw e; // just a place to put a break point for debugging.
}
}

[STAThread]
static void Main()
{
Application.Ena bleVisualStyles (); // 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.Sh owSplashScreen( ); // runs on separate thread.
Application.DoE vents();
Application.Run (new MainFrame());
}
else
{
//There is another instance of this process.
HandleRunningIn stance(instance );
}
}


Nov 16 '05 #1
4 11161
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.Threadin g 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]
- mv*@spam.guard. caspershouse.co m

"Chuck" <ce*****@austin .rr.com> wrote in message
news:ik******** ********@fe2.te xas.rr.com...
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
//
InitializeCompo nent();
try
{
Initilize(); //attempts to connect to remote database. throws if it
can't.
}
catch(System.Ne t.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,"Datab ase
Error!",Message BoxButtons.OK,M essageBoxIcon.E rror);
connectFailed = true; //needed to prevent OnLoad event handler from
doing its thing. Form loads even though I have called Exit!
Application.Exi tThread(); //This doesn't seem to work. Neither does
Application.Exi t() or this.Close();
}
catch(Exception e)
{
throw e; // just a place to put a break point for debugging.
}
}

[STAThread]
static void Main()
{
Application.Ena bleVisualStyles (); // 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.Sh owSplashScreen( ); // runs on separate thread.
Application.DoE vents();
Application.Run (new MainFrame());
}
else
{
//There is another instance of this process.
HandleRunningIn stance(instance );
}
}

Nov 16 '05 #2
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.Exi tThread() 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 [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
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.Threadin g 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]
- mv*@spam.guard. caspershouse.co m

"Chuck" <ce*****@austin .rr.com> wrote in message
news:ik******** ********@fe2.te xas.rr.com...
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
//
InitializeCompo nent();
try
{
Initilize(); //attempts to connect to remote database. throws if it
can't.
}
catch(System.Ne t.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,"Datab ase
Error!",Message BoxButtons.OK,M essageBoxIcon.E rror);
connectFailed = true; //needed to prevent OnLoad event handler from doing its thing. Form loads even though I have called Exit!
Application.Exi tThread(); //This doesn't seem to work. Neither does Application.Exi t() or this.Close();
}
catch(Exception e)
{
throw e; // just a place to put a break point for debugging.
}
}

[STAThread]
static void Main()
{
Application.Ena bleVisualStyles (); // 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.Sh owSplashScreen( ); // runs on separate thread.
Application.DoE vents();
Application.Run (new MainFrame());
}
else
{
//There is another instance of this process.
HandleRunningIn stance(instance );
}
}


Nov 16 '05 #3
Chuck <ce*****@austin .rr.com> wrote:
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.Exi t isn't
working. A simple try/catch in your Main method should work here, I
believe.

3) Use Environment.Exi t instead.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
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(EntryPoi nt).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]
- mv*@spam.guard. caspershouse.co m

"Chuck" <ce*****@austin .rr.com> wrote in message
news:gY******** *********@fe2.t exas.rr.com...
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.Exi tThread() 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 [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in message news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
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.Threadin g 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]
- mv*@spam.guard. caspershouse.co m

"Chuck" <ce*****@austin .rr.com> wrote in message
news:ik******** ********@fe2.te xas.rr.com...
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
//
InitializeCompo nent();
try
{
Initilize(); //attempts to connect to remote database. throws if
it can't.
}
catch(System.Ne t.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,"Datab ase
Error!",Message BoxButtons.OK,M essageBoxIcon.E rror);
connectFailed = true; //needed to prevent OnLoad event handler from doing its thing. Form loads even though I have called Exit!
Application.Exi tThread(); //This doesn't seem to work. Neither does Application.Exi t() or this.Close();
}
catch(Exception e)
{
throw e; // just a place to put a break point for debugging.
}
}

[STAThread]
static void Main()
{
Application.Ena bleVisualStyles (); // 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.Sh owSplashScreen( ); // runs on separate thread.
Application.DoE vents();
Application.Run (new MainFrame());
}
else
{
//There is another instance of this process.
HandleRunningIn stance(instance );
}
}



Nov 16 '05 #5

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

Similar topics

1
17702
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...
3
5086
by: ZoombyWoof | last post by:
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...
6
20082
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
1560
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
1361
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
1676
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
4114
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
9510
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
7042
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
9365
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...
0
9212
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9127
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,...
0
9056
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8002
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5973
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4748
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2569
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2127
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.