0

Possible Duplicate:
Application.Exit

I have a Winforms app in which Application.Exit() fails to run.

public Form1()
{       
   InitializeComponent();           
   path=parseINI();//Gets path from ini file           
} 

In my case, Application.Exit() is being called from the parseINI method, where it doesn't work. Is the problem that its being called when the app is starting? I stuck it in another method that runs after the form is loaded, and it works there. I did use Environment.Exit in my parseINI method, and that worked (despite it being for console apps rather than WinForms).

EDIT: I should probably add that its there as a check, to make sure a file being read is formatted correctly, if it is, then its not called, otherwise the program exits.

Community
  • 1
  • 1
Amanda_Panda
  • 1,156
  • 4
  • 26
  • 68
  • How you know it fails? Any exception? Misbehaviour? – sll Dec 19 '12 at 19:07
  • Please delete your question and see http://stackoverflow.com/questions/1057151/application-exit – sll Dec 19 '12 at 19:09
  • For the most part, using Application.Exit is a failure to properly handle the flow of your program. There are occasions where it's appropriate, but it's abused more often than it's used appropriately. – Servy Dec 19 '12 at 19:24

3 Answers3

3

This might be because you still haven't started the application via Application.Run when the Form ctor executes.

The common startup code template goes like this:

Application.Run(new Form1());

If that is the case, you call Application.Exit before Application.Run. So after you have called exit, you effectively call Run, and therefore, the application runs.

driis
  • 161,458
  • 45
  • 265
  • 341
  • Thanks, that explains some of the background for me. I also found this post which shows a possible solution: http://stackoverflow.com/a/4097152/1144431 – Amanda_Panda Dec 19 '12 at 19:22
1

Call the Form.Close() method inside parseINI() instead of Application.Exit()

Derek Tomes
  • 3,989
  • 3
  • 27
  • 41
1

The Environment.Exit() method terminates the application, closing the application thread. Indeed, from the documentation:

Terminates this process and gives the underlying operating system the specified exit code.

Why do you have to shutdown your application from within your parseINI method?

Anyway, the method call for Application.Exit() is not working for you, because, reading the MSDN page:

This method stops all running message loops on all threads and closes all windows of the application. This method does not force the application to exit. The Exit method is typically called from within a message loop, and forces Run to return. To exit a message loop for the current thread only, call ExitThread.

Then, you can add this line to your constructor, to "run" the application, allowing you to use Application.Exit():

Application.Run(new YourForm());

You can find an interesting discussion here: http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx

Read this answer for details. Furthermore, read this important SO question to better understand the difference between Environment.Exit() and Application.Exit().

Community
  • 1
  • 1
Alberto Solano
  • 7,972
  • 3
  • 38
  • 61