views:

425

answers:

5

On a Windows 2003 server I have a pure .NET 3.5 C# app (no unmanaged code). It connects to various other remote systems via sockets and acts like a data hub. It runs for 10-15 hours fine with no problem but from time to time it just disappears. If I watch the app using task manager the memory usage remains constant.

In the Main() function I wrap the invocation of the rest of the app in a try..catch block which it just blows completely past - the catch block which logs the exception to a file is ignored. If I manually raise an exception for testing, the catch block is invoked.

Prior to entering the try..catch I do ....

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);

The system has Dr. Watson on it, but nothing gets written in the directory DRWTSN32.EXE is pointing to.

How can I catch whatever exception is causing this?

A: 

If it's a Windows Forms application, you could try Application.ThreadException.

David Kemp
+5  A: 

Try using the debugging tools from microsoft. You can download them from here.

Use adplus to capture the crash and then windbg to analyze it.

adplus -crash -pn your.exe -quiet

Lots of great info about debugging on windows on this blog.

Joel Cunningham
Tess' blog is a goldmine of debugging information. The net would be a lot darker if she didn't tell us the secrets to .NET debugging.
Pure.Krome
+3  A: 

If this is a Windows Forms app, then it's likely that the unhandled exception is being caught by the window message pump, which is why you never see it. To deal with this, see my answer here.

If it's a Windows service, then the exception might be appearing on a background thread and not being marshalled back to your main thread. To deal with this, you need to marshal any background thread back to your main thread, and the exception will be re-thrown there so that you can catch it.

If it's a console app, then I'm a bit mystified.

EDIT: Your comment says this is a Windows Forms app. In that case, you're probably not seeing the exception because it's being handled by the built-in Windows Forms exception handler that does the following by default:

  • Catches an unhandled managed exception when:
    • no debugger attached, and
    • exception occurs during window message processing, and
    • jitDebugging = false in App.Config.
  • Shows dialog to user and prevents app termination.

You can disable this behaviour by setting jitDebugging = true in App.Config. Then you should be able to see the unhandled exception by registering for the event Application.ThreadException, e.g. in C#:

Application.ThreadException += new Threading.ThreadExceptionHandler(CatchExceptions);
RoadWarrior
It's a winforms app with one form
rc1
A: 

You could also attach WinDBG at startup and enable breakpoints on .NET exceptions. You can then do a !printexception to see what is going on.

Cory Foy
+1  A: 

There might be trace of the application in the EventLog.

I have had a .Net app disappear without the possibility to catch an Exception. There was an entry in the EventLog every time this happened.

To view the eventlog just type EventVwr on the command prompt, or run box.

GvS
The submitter might not be aware of the existence of the EventLog. Mayhaps add a hint on how to access it.
Deestan
Thx for the remarks. Changed.
GvS