Resolved How to close a console application window

mauede

Well-known member
Joined
Sep 1, 2021
Messages
103
Location
Northwood - UK
Programming Experience
Beginner
Dear C# Experts,

I am developing console applications that use Velocity API from Varian Medical Systems.
The user interface is simply a terminal window (see attachment)
At the end of the script, I would like to close the terminal window.
I tried several statements:
  • return
  • Environment.Exit(0)
  • System.Environment.Exit(0)
  • Application.Exit(0)
None works either with Visual Studio debugger or even when I run the executable.

Thank you for your help.

Regards,
maura.monville@gmail.com
 

Attachments

  • Console_terminal-window.PNG
    Console_terminal-window.PNG
    55.6 KB · Views: 418
Console windows are not owned by the program. The program just runs within the window.

If you have the compiled .EXE of a console application, if you double click on it, the Windows will create a console window for it to run in, and when the program is done, the console window will close automatically. On the other hand, if you open a console window, and then run the .EXE, the console window will stay open after running.

In Visual Studio, the old default was that if you are debugging a console application, the console window will close automatically after running it. But if you are running a console application, the console window will be held open by Visual Studio until you press a key to close it. In newer version of Visual Studio (starting 2017, I believe), Options>Debugging>General has the checkbox for "Automatically close the console when debugging stops" where you can control this.
 
Thank you so much.

I found the option to close the terminal window when closing the debugger.
Unluckily, when I run the executable the terminal window is not closed whether the program exits because of a
captured exception or it completes successfully.
I do not open a window explicitly. the Console application opens up a window upon starting.

At present my biggest problem is to publish my code on a shared drive. Maybe I should post a new question.
I admit my ignorance about Windows. I deployed my code successfully on other platforms (Linux).
I get confused by the question asked by the Visual Studio publishing procedure. If I publish without signing then Visual Studio tells me that the signature is different from the previous release. The publication process completes without any error but the executable does not start from the remote shared drive.
If I publish with signature again no publication error. When I try to run the executable from the remote location the system tells me that the application cannot be started because it lacks some files.
I swear my program runs with and without the debugger on the workstation where I developed it.
A nightmare!
 
You should open another thread regard that ClickOnce deployment you are trying to do.
 
when I run the executable
How exactly are you running the executable?
In Visual Studio, pressing Ctrl-F5?
In File Explorer, double clicking on the compiled .EXE?
 
Console windows are not owned by the program. The program just runs within the window.

If you have the compiled .EXE of a console application, if you double click on it, the Windows will create a console window for it to run in, and when the program is done, the console window will close automatically. On the other hand, if you open a console window, and then run the .EXE, the console window will stay open after running.

In Visual Studio, the old default was that if you are debugging a console application, the console window will close automatically after running it. But if you are running a console application, the console window will be held open by Visual Studio until you press a key to close it. In newer version of Visual Studio (starting 2017, I believe), Options>Debugging>General has the checkbox for "Automatically close the console when debugging stops" where you can control this.
OK with the Debugger.
However, my code is not capable of closing the Console window despite trying different instructions. Please, see the screenshots I attached to my last posted message.
I do not start the executable from a console that I open prior to running the executable.
It is the executable that pops up a Console window. In fact, it is a console application.
 
Console applications close their own window if they pop open their own window.

This closes by itself for me:
ConsoleApp1:
using System;
using System.Threading;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World.");
            Console.WriteLine("Closing in 5 seconds...");
            Thread.Sleep(5000);
        }
    }
}

Create Windows Console project in Visual Studio. (Doesn't matter if .NET Framework or .NET Core.)
Paste the code above into the Program.cs.
Build.Build Solution
In the Windows File Explorer browse to the location that contains the resulting .EXE.
Double click on the .EXE.

You'll see a console window appear that says "Hello, World." "Closing in 5 seconds...". After 5 seconds the window will close by itself.
 
Back
Top Bottom