473,387 Members | 1,493 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,387 software developers and data experts.

catching unhandled exceptions and auto-rebooting an application.

Hi All,

I have seen a few good behaviours for what to do in the case of those pesky
uncaught exceptions. and I want to build some of these mechanisms into my
Application that is currently out on the factory floor [enhancing peoples
lives - said almost hysterically].

What I'd like to do is catch that uncaught exception .. log the exception
info and then pop up a window that is counting down to an application re-start
that has a space for the operator to enter some info explaning just what
the crikeyJingo he did to my application... (grrrr users really delight in
blowing up apps .. and no amount of easter eggs placate them *sigh*) This
way I can

Problem / Opportunity 1:

What I dont know how to do is catch that uncaught exception. more specifically
'where' to add the handler. I use VS2005 and create my projects just using
the defaults. I'm assuming it probably needs to go in that poor forgotten
Program.cs file that I never look at ... or maybe not.

Can any-one please enlighten me on exactly what handler to create and more
impotantly WHERE do I put it (given a VS2005 default solution).

Problem / Opportunity 2:

The second problem is finding a neat way to restart the application. I have
a number of service apps that support the main app so I am expecting that
I shall get one of these to run a new instance of the app After its been
notified (as part of the above 'closing down' behaviour ) that I would like
a restart-thank-you-very-much ...

Is there as better way ? is there a .net pattern ? is there a "1-click create
application" with property "non-trivial" set ..
---------------------------------------------
[Sam Samson] - There is no greater folly than fixed price quoting.
Aug 29 '07 #1
5 1673
You can handle the appdomain unhandled exception event for instance. You
won't be able to show a window there because the domain is unloading.
Basically, it's too late.

For the restart, you need something like a watch dog application that knows
how to restart your application when it detects that it isn't loaded.

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET - MS Press
Professional VSTO 2005 - Wrox/Wiley
OWC Black Book www.lulu.com/owc

"Sam Samson" <no*******@nospam.com.auwrote in message
news:ec*************************@news.microsoft.co m...
Hi All,

I have seen a few good behaviours for what to do in the case of those
pesky uncaught exceptions. and I want to build some of these mechanisms
into my Application that is currently out on the factory floor [enhancing
peoples lives - said almost hysterically].

What I'd like to do is catch that uncaught exception .. log the exception
info and then pop up a window that is counting down to an application
re-start that has a space for the operator to enter some info explaning
just what the crikeyJingo he did to my application... (grrrr users really
delight in blowing up apps .. and no amount of easter eggs placate them
*sigh*) This way I can
Problem / Opportunity 1:

What I dont know how to do is catch that uncaught exception. more
specifically 'where' to add the handler. I use VS2005 and create my
projects just using the defaults. I'm assuming it probably needs to go in
that poor forgotten Program.cs file that I never look at ... or maybe not.

Can any-one please enlighten me on exactly what handler to create and more
impotantly WHERE do I put it (given a VS2005 default solution).

Problem / Opportunity 2:

The second problem is finding a neat way to restart the application. I
have a number of service apps that support the main app so I am expecting
that I shall get one of these to run a new instance of the app After its
been notified (as part of the above 'closing down' behaviour ) that I
would like a restart-thank-you-very-much ...
Is there as better way ? is there a .net pattern ? is there a "1-click
create application" with property "non-trivial" set ..

---------------------------------------------
[Sam Samson] - There is no greater folly than fixed price quoting.


Aug 29 '07 #2
"Sam Samson" <no*******@nospam.com.auwrote in message
news:ec*************************@news.microsoft.co m...
Hi All,

I have seen a few good behaviours for what to do in the case of those
pesky uncaught exceptions. and I want to build some of these mechanisms
into my Application that is currently out on the factory floor [enhancing
peoples lives - said almost hysterically].

What I'd like to do is catch that uncaught exception .. log the exception
info and then pop up a window that is counting down to an application
re-start that has a space for the operator to enter some info explaning
just what the crikeyJingo he did to my application... (grrrr users really
delight in blowing up apps .. and no amount of easter eggs placate them
*sigh*) This way I can
Problem / Opportunity 1:

What I dont know how to do is catch that uncaught exception. more
specifically 'where' to add the handler. I use VS2005 and create my
projects just using the defaults. I'm assuming it probably needs to go in
that poor forgotten Program.cs file that I never look at ... or maybe not.

Can any-one please enlighten me on exactly what handler to create and more
impotantly WHERE do I put it (given a VS2005 default solution).
Add a handler to Application.ThreadException.
>
Problem / Opportunity 2:

The second problem is finding a neat way to restart the application. I
have a number of service apps that support the main app so I am expecting
that I shall get one of these to run a new instance of the app After its
been notified (as part of the above 'closing down' behaviour ) that I
would like a restart-thank-you-very-much ...
This might work:

Process.Start(Environment.CommandLine);

You might run into problems if the command line contains arguments. In that
case you would have to look at Environment.GetCommandArgs() and call
Process.Start(command, args).
>
Is there as better way ? is there a .net pattern ? is there a "1-click
create application" with property "non-trivial" set ..
Is there a better way? Probably.
Aug 29 '07 #3
After writing I found some techniques to do what I wanted.

1:

static void Main()
{
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Appli cation_ThreadException);

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new Form1());
}

and 2:

Application.Restart();

But as was rightly pointed out ... I have nada to capture any information
with ... (but a great start all the same ) ... is there any compelling reason
why I could not instantiate another small, purpose built Form2 i.e. Application.Run(new
Form2()); capture my debug info then restart ?

Cheers Sam
Aug 29 '07 #4
Well my compelling reason is .. it just doesnt work ... not allowed to have
2 message pumps on the same thread ... hmmm sounds bad I better not do that.
However the compiler suggested Form.ShowDialog() .. which has given me 'some'
hope ..

I tried the following and now I'm completely stuck ...

static class Program
{
Form1 myForm; // <-- doesnt like this much

[STAThread]
static void Main()
{
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Appli cation_ThreadException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
myForm = new Form1();
Application.Run(myForm);
}

static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs
e)
{
MessageBox.Show(e.Exception.Message);

myForm.ShowDialog(new Form2()); // <-- therefore wont do this

MessageBox.Show("DID FORM2 LOAD");
Application.Restart();
}
}
Aug 29 '07 #5
On 29 Aug., 04:42, Sam Samson <nospam...@nospam.com.auwrote:
Well my compelling reason is .. it just doesnt work ... not allowed to have
2 message pumps on the same thread ... hmmm sounds bad I better not do that.
However the compiler suggested Form.ShowDialog() .. which has given me 'some'
hope ..

I tried the following and now I'm completely stuck ...

static class Program
{
Form1 myForm; // <-- doesnt like this much

[STAThread]
static void Main()
{
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Appli cation_ThreadException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
myForm = new Form1();
Application.Run(myForm);
}

static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs
e)
{
MessageBox.Show(e.Exception.Message);

myForm.ShowDialog(new Form2()); // <-- therefore wont do this

MessageBox.Show("DID FORM2 LOAD");
Application.Restart();
}
}
Programm is a static class, this will prevent you from declaring an
instance variable (myForm in this case).
In addition, "myForm.ShowDialog(new Form2())" seems weird, you create
a new instance of Form2, set it as the parent of myForm and then show
myForm, probably not what you intended to do.
I actually don't have much experience with this sort of problem
(restarting an app after it crashed), but I doubt this is the best way
of doing it, even if you get it to work.

hth,
Kevin Wienhold

Aug 29 '07 #6

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

Similar topics

1
by: Rolf | last post by:
I understand a compilation error occurs when a method that throws no exceptions is the only code in a try block. What I don't understnad is why I can specify the catching of an Exception for a...
2
by: Keith Bolton | last post by:
I am handling exceptions currently using try, except. Generally I don't handle specific exceptions and am catching all. Then if an exception occurs, I would like to capture that error string....
4
by: Laxmikant Rashinkar | last post by:
Hi, I am working with a program that someone else wrote. This program is full of bugs and unhandled exceptions are thrown all over the code. Instead of trying to catch the exception at each...
12
by: Vasco Lohrenscheit | last post by:
Hi, I have a Problem with unmanaged exception. In the debug build it works fine to catch unmanaged c++ exceptions from other dlls with //managed code: try { //the form loads unmanaged dlls...
0
by: Chris Joslyn | last post by:
I am integrating some legacy VC++ 6.0 classes into a managed C++.NET Windows service. After I had the basic framework in place and running, I added in the source files I needed and tried to run the...
7
by: Derek Schuff | last post by:
I'm sorry if this is a FAQ or on an easily-accesible "RTFM" style page, but i couldnt find it. I have some code like this: for line in f: toks = line.split() try: if int(toks,16) ==...
3
by: Mike Binks | last post by:
I wish to make sure all unhandled exceptions (UE) are handled by a central handler. For primary thread UEs, this may be done by string exceptionText; ...
0
by: Amil Hanish | last post by:
I am using VS 2005 and .NET 2.0. I am trying to catch exceptions in outside the context of a page request (i.e. timer or background thread). I am following the MS instructions at: ...
0
by: Amil Hanish | last post by:
I am using VS 2005 and .NET 2.0. I am trying to catch exceptions outside the context of a page request (i.e. timer or background thread). I am following the MS instructions at: ...
12
by: Karlo Lozovina | last post by:
I'm not sure if Python can do this, and I can't find it on the web. So, here it goes: try: some_function() except SomeException: some_function2() some_function3() ...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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...

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.