views:

70

answers:

5

on closing the main form of the application(which I call so many methods on it,etc) if I run my application from IDE and want to close it, it is ok but if I just run the exe file It will throw an exception.

so what way do you propose to me for debugging it? as I said when I run it from the IDE, it is Ok and no error occurs

+1  A: 

I have a solution written in C++-CLI which should be easy enough to port to C#.

If it's happening within the main function itself, have you tried wrapping all your code in a:

try
{
    main();
}
catch( System.Exception^ e)
{
    // do something
}

Apologies for the C++-ish-ness of my answer - it's been a long time since I wrote any C# ;-)

Jon Cage
+2  A: 

Find out what the exception is, to start with. Can you already see the exception details? Does it offer you the option of attaching to the debugger? Can you catch the exception and log it?

Jon Skeet
Thanks, Attach To Process technique worked, sorry I had to accept another earlier answer which also proposes the Attach To Process Technique
BDotA
+3  A: 

Two things I can think of to try:

  • Run the application from outside the IDE but then attach to the process. It could be that when starting from the debugger the environment will be different in some way
  • Use adplus (see my earlier post here to catch the crash dump so you can analyse it later
the_mandrill
+2  A: 

Attach the debugger after you got the program started. This ensures any side-effects, like the startup directory, the hosting process and JIT optimization cannot be affected by the debugger.

Start your program. Tools + Attach to Process.

Hans Passant
Thanks, Attach To Process technique worked, sorry I had to accept another earlier answer which also proposes the Attach To Process Technique
BDotA
Upvotes have been hard to come by today, I'm ahead.
Hans Passant
+1  A: 

You should be able to attach a global exception handler:

Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

And then create a function to handle the exception:

private void Application_ThreadException(object sender,System.Thread.ThreadExceptionEventArgs e) {
    // Do whatever here
}
icemanind
Thanks, Attach To Process technique worked, sorry I had to accept another earlier answer which also proposes the Attach To Process Technique
BDotA
That sounds interesting. What is the best place in code that I can add this?
BDotA
Do it either in the Load Event or the constructor
icemanind