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);
    }
11
  • 1
    Can you post some code? Commented Nov 7, 2016 at 9:38
  • I updated the question Commented Nov 7, 2016 at 9:40
  • 1
    Or just return if you are in the Main method. Commented Nov 7, 2016 at 9:41
  • 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? Commented Nov 7, 2016 at 9:43
  • 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. Commented Nov 7, 2016 at 9:54

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

try Application.Current.Shutdown()

2 Comments

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'
Why do you have both Windows forms and WPF assemblies referenced in a console application?
-1

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.

10 Comments

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 I didn't talk about Application.Exit - did you comment in the right place?
return x; does not exit the application
It doesn't exit the application or it doesn't exit the console window? Because returning from the main method always ends the application.
It does not close the application Console Window after finishing its task
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.