tags:

views:

43

answers:

3

Hi,

I have an app, and after about 20 minutes of idle time the program just crashes. ("Windows has encountered an error and needs to close...")

I have no idea why this is happening. What is the best way to go about debugging something like this?

+2  A: 

Did you debug your code? Did you check EventLog? Because, when you debug and didnt find any exception, the best way to see whats happened is checking EventLog.

Can I see your code? You know, a lots of things can crash your app. Virus, your o/s, your code etc.. We need to see your code to help your more :)

Serkan Hekimoglu
How do I do this?
whydna
Computer > RightClick > Manage > Event Viewer. Keep your eyes on logs, when your program crashed.
Serkan Hekimoglu
and please post your codes, if possible. Lets see whats happening. Maybe you forgot to put try catch in anywhere..
Serkan Hekimoglu
+1  A: 

Maybe info from this can help you?

Daniel Mošmondor
+2  A: 

Generally crashes in .Net applications are caused by an unhandled exception - i.e. an exception in a thread in that application that was not caught in a try-catch block of some sort:

try
{
    // Some code that may throw an exception
}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString());
}

A good first place to check for information about this exception is the application event log, often however you will find that the .Net framework posts only minimal information about the crash - if this is the case then you need to catch and handle the exception yourself, recording enough information to allow you to diagnose the error.

Typically there are two way that you might do this:

1. Ensure that the code for each thread of your application is contained in a try-catch block.

This is the easiest method - unless your application has multiple user threads (you will know if it has), this simply requires that you place a try-catch block around the entry point of your application, for example in a Windows Forms application:

// Probably found somewhere in Program.cs
[STAThread]
static void Main()
{
    try
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

If you are working on a console application then you will need to use something other than MessageBox (see the first example).

If you spawn multiple threads then the entry point method for each thread should also catch all exceptions in a similar way.

2. Handle the UnhandledException event of the current App Domain

This event will be fired whenever any thread throws an unhandled exception in the current App Domain. Generally speaking it is best to use the first method instead, however this event is still useful in some situations.

Example:

static void Main()
{
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    // The rest of your application
}

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    Console.WriteLine(e.ExceptionObject.ToString());
}

Of course it is worth pointing out that the error still might not be caught by either of the above two methods (which is possible if the error is caused by a native module loaded in the same process), however this should probably do the trick.

Finally - Good luck!

Kragen