locked
Application unstable RRS feed

  • Question

  • I've been programming (hobby) software in Java for a couple of years. Recently I switched to .NET and found out that .NET applications are generally less stable than those written in Java. They often crash when I try to close the main form and they crash more often now as the size of the application gets bigger. I never had those problems in Java even with 5000-line programs. I use this to exit the application when I close the main form:
    [code]
    this.Closing+=delegate{ Environment.Exit(0); };
    [/code]
    Is it because I dont follow general software-engineering principles or because of something else?
    Friday, January 16, 2009 9:29 PM

Answers

  • Run your program, close it so it is zombied.  Switch to the IDE, Tools + Attach to Process.  Select your running process, Debug + Break All.  Look at the call stacks of the threads.
    Hans Passant.
    • Marked as answer by Linda Liu Wednesday, January 21, 2009 7:36 AM
    Saturday, January 17, 2009 9:42 PM

All replies

  • Calling Environment.Exit() in the Closing event is a Bad Idea.  Just don't, it doesn't need any help.
    Hans Passant.
    • Proposed as answer by Javier Campos Saturday, January 17, 2009 7:55 AM
    Saturday, January 17, 2009 2:13 AM
  • nobugz is right, but I'd say even more: calling Environment.Exit from a Windows.Forms application is never a good idea (doing it on the Closing event is just worse). If you need to terminate an application which was started using Application.Run() (i.e., has a message loop), do it by closing its main form if it has one (preferred way), or with Application.Exit() if it doesn't;



    Before I speak, I have something important to say.
    Saturday, January 17, 2009 7:54 AM
  • If I dont call Environment.Exit() when the main form is closed, the application remains in the process list even when it is closed. So it wont start the next time (if I change the source file and recompile).
    It says something like
    [quote]Could not write to output file 'd:\My Documents\Visual Studio 2008\Projects\Program1\obj\Debug\Program1.exe' -- 'The process cannot access the file because it is being used by another process. ' (CS0016)[/quote]
    So I need to go to Task Manager and kill the process manually.
    If that's what causing the problem, is there another way to terminate the application?
    Saturday, January 17, 2009 5:34 PM
  • This happens when you use a thread and you don't shut it down when your main form closes.  That thread would also be a likely source of exceptions.
    Hans Passant.
    Saturday, January 17, 2009 6:08 PM
  • Shouldnt the application clean up everything after it exits?
    Either way, Im not using any threads in my application (except maybe those that are internal to controls).
    Saturday, January 17, 2009 6:24 PM
  • Remove the Exit() call, run, close your form.  Switch back to VS and use Debug + Break.  Look at the call stack of the threads to see what's running.
    Hans Passant.
    Saturday, January 17, 2009 6:41 PM
  • Im using #Develop (sorry for confusion with the project path).
    Saturday, January 17, 2009 6:54 PM
  • You're on your own with that, only Visual Studio is supported here.
    Hans Passant.
    Saturday, January 17, 2009 7:05 PM
  • Ok, I loaded it in Microsoft Visual C# 2008 Express Edition.
    Im not seeing Debug + Break option. Only Break All.
    Also, closing the form stops debugging...
    Saturday, January 17, 2009 7:55 PM
  • Ah, good.  Problem solved.
    Hans Passant.
    Saturday, January 17, 2009 8:32 PM
  • No it is not solved (not completely).
    If I remove Environment.Exit(), I can run in debug mode after recompiling (which I cant do if I run without the debugger). If I run the project outside of the IDE (by double clicking the .exe) it often (about 50%) of the time does not remove the process from the Task Manager. So if I open and then close Program1.exe 10 times, my Task Manager will contain 4-6 Program1.exe processes. This is not a very big problem (given todays GB rams) though it is quiet annoying.
    Saturday, January 17, 2009 8:58 PM
  • Run your program, close it so it is zombied.  Switch to the IDE, Tools + Attach to Process.  Select your running process, Debug + Break All.  Look at the call stacks of the threads.
    Hans Passant.
    • Marked as answer by Linda Liu Wednesday, January 21, 2009 7:36 AM
    Saturday, January 17, 2009 9:42 PM