How to exit from a console application

CK CK 3788 
Created at
Updated at  
812 0 0

Environment.Exit in C# is a static method that abruptly terminates the current process, exiting the application immediately.

It takes an integer argument representing the exit code, which can be used to signal success (0) or failure (non-zero) to the operating system or other programs.

While Environment.Exit provides a way to terminate the program, it's generally considered good practice to use the more controlled approach of returning from the Main method or using the Application.Exit method in Windows Forms or WPF applications. 

Use Environment.Exit to immediately return from a console application with a specified result code.
 

using System;

namespace ConsoleApplication1
{
  class Class1
  {
    [STAThread]
    static void Main(string[] args)
    {
      Console.WriteLine("About to exit");

      int exitCode = 0;
      Environment.Exit(exitCode);

      // This line is not executed.
      Console.WriteLine("After exit");
    }
  }
}


 

Tags Application.Exit C# Exit C# Terminate Application Disable Button Environment.Exit Exit Terminate Application Facebook X
Comments 0
Similar posts
  1. How to return exit code such as exit() function in C/C++ ?
    666
  1. How to convert bitmap image to jpeg image ?
    356
  2. To load image from file
    452
  3. URL Encode, Decode function for MFC
    605
  4. How to load HTML resource on MFC ?
    842
  5. How to get GM Time as String ?
    423
  6. Makefile to automate the build process for C++
    280
  7. Finding process name on OS
    409
  8. Convert System.DateTime to UNIX timestamp
    381
  9. Retrieve system information
    495
  10. How to call SetTimer function on MFC CDialog class ?
    651
  11. How to rename(change file name) in C# ?
    1,080
  12. Find Files in certain directory
    928
  13. Implementing C#'s foreach loop in Delphi 8
    460
  14. How to detect resource leaks in a Form
    940
  15. The best way to enable and disable buttons, menu items and toolbar buttons
    667
  16. How to restrict a program to a single instance
    729
  17. Compress and decompress strings in C#
    972
  18. Graphics in C# - Irregular Forms
    910
  19. Extracting the Country from IP Address
    469
  20. How to run shell command by MFC ?
    439
  21. Binary Search Sample Code
    407
  22. Double Linked List based in C++
    796
  23. String Replace Function For C#
    1,550
  24. How to print character in C# by ASCII code
    477
  25. MFC based World Wide Web HTTP Server Source Code
    460
  26. Creating and Using a CAsyncSocket Object to use CAsyncSocket
    472
  27. UDP Send and Receive Using CAsyncSocket
    1,204
  28. Using the shell to receive notification of removable media being inserted or removed
    681
  29. Adding Custom Paper Sizes To Named Printers
    649
  30. Print HTML In C# With Or Without The Web Browser Control And The Print Dialog
    972
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    42
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    36
  3. How do I create animated background for Google Meet?
    21,044
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    252
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  7. Array of arguments passed to script in PHP
    90
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  10. The Two Clocks of Ad Server Architecture
    71
  11. Frequency Cap Architecture Overview
    515
  12. Collecting Proof-of-Play from 100,000 Screens
    146
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    288
  14. What is RedisVL?
    109
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    232
  16. TPU & GPU Cluster Management Tools
    245
  17. The Concept of Scaffolding in AI Agents
    234
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    259
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  22. Machine Learning: Training vs. Ranking
    222
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  25. Designing a Data Curation System Around a Vector Database
    221
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    257
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    196
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    219
  30. Understanding Data Lineage
    263