By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,201 Members | 920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,201 IT Pros & Developers. It's quick & easy.

catching unhandled exceptions and auto-rebooting an application.

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.