catching unhandled exceptions and auto-rebooting an application.

S

Sam Samson

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 ..
 
A

Alvin Bruney [MVP]

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.
 
J

John Vottero

Sam Samson said:
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.
 
S

Sam Samson

After writing I found some techniques to do what I wanted.

1:

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

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
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
 
S

Sam Samson

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(Application_ThreadException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
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();
}
}
 
K

KWienhold

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(Application_ThreadException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
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
 

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