1

I have a console Application Program on Visual Studio 2013. .NET 4.5 I want the program to quit after doing a Certain task. SO, I use System.Environment.Exit(0); but it does not exit. It only displays on the console: Press any key to continue.

How can i exit the Program? Thanks in Advance.

Here iam checking if directory is empty, then exit the program, I also tried to put System.Environment.Exit(0); in the main function but it did nothing.

    if (Directory.GetFiles(@"Q:\").Length == 0)
    {
        System.Environment.Exit(0);
    }
Mike
  • 35
  • 1
  • 7
  • 1
    Can you post some code? – Bojan B Nov 07 '16 at 09:38
  • I updated the question – Mike Nov 07 '16 at 09:40
  • 1
    Or just `return` if you are in the `Main` method. – Thorsten Dittmar Nov 07 '16 at 09:41
  • You should use Application.Exit(). Environment.Exit would only kill the process. Check this [post](http://stackoverflow.com/questions/13046019/winforms-application-exit-vs-enviroment-exit-vs-form-close) – kashi_rock Nov 07 '16 at 09:43
  • 5
    *It only displays on the console: Press any key to continue.* Looks like the standard behavior when you run a console app from the IDE. Does the same thing happen if you run the executable directly? – Frédéric Hamidi Nov 07 '16 at 09:43
  • @kashi_rock No. This does not work for console applications. – Thorsten Dittmar Nov 07 '16 at 09:43
  • Could it be that your program has additonal non-background threads running? These might keep the process running. – stakx - no longer contributing Nov 07 '16 at 09:48
  • _Press any key to continue_ and it waits -- it sounds like a message generated by your IDE when your app terminates. Really, I remember it since at least the time I started with QBasic, but can't really remember if Borland products had the same behaviour. :) – Lyubomyr Shaydariv Nov 07 '16 at 09:49
  • 3
    Happens when you press Ctrl+F5 to start your program instead of F5. Feature, not a bug, it lets you look at your program output before it closes the console window. Without that it would be flash, bang, gone. As would happen when you start your program from a shortcut on your desktop. – Hans Passant Nov 07 '16 at 09:54
  • just return in the main method does not exit the console / Program – Mike Nov 07 '16 at 09:58
  • Exiting the console is different from exiting the program. *Any* C# program will exit when you leave the main method. As others have suggested, this may be a feature. – Thorsten Dittmar Nov 07 '16 at 10:49

3 Answers3

2
Press any key to continue

Like a comment above said already, this sounds a lot like what your IDE (presumably Visual Studio) would print out once the process has terminated. On my machine, I get this message when I run a console application without attaching the debugger to it, e.g. by starting it with Ctrl+F5. Once I press a key, the console window spawned by Visual Studio will close. This is expected behaviour.

Try running your program with the debugger attached, e.g. by pressing F5. Or start the program from a command prompt instead of starting it from your IDE. The message shouldn't be displayed then.

Note also that if you have a multi-threaded program, and there are non-background threads running, these can keep a process alive even though the main thread has terminated.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
0

The most graceful way to exit a console application is to return from the Main method.

Simply declare your main method as follows:

public static int Main(string[] args)

and you can use

return x;

to exit the application with the given return code.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • Application.Exit gives me these two errors: Error 2 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement Error 1 'Application' is an ambiguous reference between 'System.Windows.Forms.Application' and 'System.Windows.Application' – Mike Nov 07 '16 at 09:53
  • @Mike I didn't talk about `Application.Exit` - did you comment in the right place? – Thorsten Dittmar Nov 07 '16 at 09:55
  • return x; does not exit the application – Mike Nov 07 '16 at 09:56
  • It doesn't exit the application or it doesn't exit the console window? Because returning from the main method *always* ends the application. – Thorsten Dittmar Nov 07 '16 at 10:17
  • It does not close the application Console Window after finishing its task – Mike Nov 07 '16 at 10:47
  • Well, does the console window close when you start your program outside Visual Studio? As many others have mentioned, this is a feature when you use Ctrl+F5 in VS to run your program so you can see the output. – Thorsten Dittmar Nov 07 '16 at 10:48
  • When i run the release version, the window closes, but when from the application itself, i press Ctrl and F5, the window does not close after the task of the program is finished – Mike Nov 07 '16 at 10:53
  • Well - did you *read* the other comments telling you that this is **normal, desired behavior** in Visual Studio? – Thorsten Dittmar Nov 07 '16 at 10:56
  • So, this means that the application exits although the console does not close? – Mike Nov 07 '16 at 10:56
  • YES!! As you would have noticed had you run it outside Visual Studio or through F5 instead of Ctrl+F5. – Thorsten Dittmar Nov 07 '16 at 10:57
0

try Application.Current.Shutdown()

Ghost Developer
  • 1,283
  • 1
  • 10
  • 18
  • Error 2 'System.Windows.Forms.Application' does not contain a definition for 'Current' Error 1 'Application' is an ambiguous reference between 'System.Windows.Forms.Application' and 'System.Windows.Application' – Mike Nov 07 '16 at 09:55
  • Why do you have both Windows forms and WPF assemblies referenced in a console application? – Thorsten Dittmar Nov 07 '16 at 10:18