WinForms app exit suddenly

D

deerchao

Hi, there.
I'm developing a WinForms application, and a user reported that it
will exit suddenly seconds after startup, every time. There isn't any
prompt at all, just like he killed the process from task manager. I
think I catched most of possible exceptions, and in case it's because
of an uncatched exception, there should be something like "Do you want
to send report to Microsoft" dialog.
In my app, I used Thread, FileSystemWatcher, WebClient, and gloabl
keyboard hook depends on user's setting. Is there any chance that
these components will cause this behaviour?
My app is developed on .Net 2.0, and my user is using Windows XP sp2
with .Net 3.0 installed.

Thanks!
 
G

Guest

Hi,
You need to hook up handlers for both the System.Windows.Forms.Application.
ThreadException event and the System.appdomain.UnhandledException event. Both
of these events need to be hooked up, as the WinForms support in the
Framework does a lot of exception trapping itself. It exposes the
System.Windows.Forms.Application.ThreadException event to allow you to get
any unhandled exceptions that happen on the UI thread that the WinForms and
their events are running on. In spite of its deceptive name, the
System.Windows.Forms.Application.ThreadException event handler will not catch
unhandled exceptions on worker threads constructed by the program or from
ThreadPool threads. In order to catch all of those possible routes for
unhandled exceptions in a WinForms application, you need to hook up a handler
for the System.appdomain.UnhandledException event that does catch those (but
not the UI thread ones that System.Windows.Forms.Application.ThreadException
does).
For more info refer below links:
http://www.codeproject.com/dotnet/UnhandledExceptionClass.asp
http://www.doogal.co.uk/exception.php
 
M

Michael Nemtsev [MVP]

Hello deerchao,

how do you handle unhandled exceptions?
do u use AppDomain.UnhandledException event?

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


d> Hi, there.
d> I'm developing a WinForms application, and a user reported that it
d> will exit suddenly seconds after startup, every time. There isn't any
d> prompt at all, just like he killed the process from task manager. I
d> think I catched most of possible exceptions, and in case it's because
d> of an uncatched exception, there should be something like "Do you
d> want
d> to send report to Microsoft" dialog.
d> In my app, I used Thread, FileSystemWatcher, WebClient, and gloabl
d> keyboard hook depends on user's setting. Is there any chance that
d> these components will cause this behaviour?
d> My app is developed on .Net 2.0, and my user is using Windows XP sp2
d> with .Net 3.0 installed.
d> Thanks!
d>
 
D

deerchao

Hello deerchao,

how do you handle unhandled exceptions?
do u use AppDomain.UnhandledException event?

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog:http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

d> Hi, there.
d> I'm developing a WinForms application, and a user reported that it
d> will exit suddenly seconds after startup, every time. There isn't any
d> prompt at all, just like he killed the process from task manager. I
d> think I catched most of possible exceptions, and in case it's because
d> of an uncatched exception, there should be something like "Do you
d> want
d> to send report to Microsoft" dialog.
d> In my app, I used Thread, FileSystemWatcher, WebClient, and gloabl
d> keyboard hook depends on user's setting. Is there any chance that
d> these components will cause this behaviour?
d> My app is developed on .Net 2.0, and my user is using Windows XP sp2
d> with .Net 3.0 installed.
d> Thanks!
d>

Thanks, I'll try System.appdomain.UnhandledException. Currently I only
hooked System.Windows.Forms.Application.
ThreadException.
 
D

deerchao

Hi,
You need to hook up handlers for both the System.Windows.Forms.Application.
ThreadException event and the System.appdomain.UnhandledException event. Both
of these events need to be hooked up, as the WinForms support in the
Framework does a lot of exception trapping itself. It exposes the
System.Windows.Forms.Application.ThreadException event to allow you to get
any unhandled exceptions that happen on the UI thread that the WinForms and
their events are running on. In spite of its deceptive name, the
System.Windows.Forms.Application.ThreadException event handler will not catch
unhandled exceptions on worker threads constructed by the program or from
ThreadPool threads. In order to catch all of those possible routes for
unhandled exceptions in a WinForms application, you need to hook up a handler
for the System.appdomain.UnhandledException event that does catch those (but
not the UI thread ones that System.Windows.Forms.Application.ThreadException
does).
For more info refer below links:http://www.codeproject.com/dotnet/UnhandledExceptionClass.asphttp://www.doogal.co.uk/exception.php
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

Thanks, it worked.

It's because I copied some code to detect if the computer is connected
to network in a background thread:

static bool IsConnectedToInternet()
{
bool connected = SystemInformation.Network;
if (connected)
{
connected = false;
System.Management.ManagementObjectSearcher searcher =
new System.Management.ManagementObjectSearcher("SELECT
NetConnectionStatus FROM Win32_NetworkAdapter");
foreach (System.Management.ManagementObject
networkAdapter in searcher.Get())
{
if (networkAdapter["NetConnectionStatus"] != null)
{
if
(Convert.ToInt32(networkAdapter["NetConnectionStatus"]).Equals(2))
{
connected = true;
break;
}
}
}
searcher.Dispose();
}
return connected;
}

and there is a ManagementException thrown.

Is there any more solid way to do this?

Thanks!
 

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