Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 54777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:16:26+00:00 2026-05-10T17:16:26+00:00

What is the best way to keep a console application open as long as

  • 0

What is the best way to keep a console application open as long as the CancelKeyPress event has not been fired?

I would prefer to not use Console.Read or Console.ReadLine as I do not want to accept input. I just want to enable the underlying application to print to the console event details as they are fired. Then once the CancelKeyPress event is fired I want to gracefully shut down the application.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:16:26+00:00Added an answer on May 10, 2026 at 5:16 pm

    I’m assuming that ‘gracefully shut down the application’ is the part you are struggling with here. Otherwise your application will automatically exit on ctrl-c. You should change the title.

    Here’s a quick demo of what I think you need. It could be refined a bit more with use of locking and Monitors for notification. I’m not sure exactly what you need though, so I’ll just pose this…

    class Program {      private static volatile bool _s_stop = false;      public static void Main(string[] args)     {         Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);         while (!_s_stop)         {             /* put real logic here */             Console.WriteLine('still running at {0}', DateTime.Now);             Thread.Sleep(3000);         }         Console.WriteLine('Graceful shut down code here...');          //don't leave this...  demonstration purposes only...         Console.ReadLine();     }      static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)     {         //you have 2 options here, leave e.Cancel set to false and just handle any         //graceful shutdown that you can while in here, or set a flag to notify the other         //thread at the next check that it's to shut down.  I'll do the 2nd option         e.Cancel = true;         _s_stop = true;         Console.WriteLine('CancelKeyPress fired...');     }  } 

    The _s_stop boolean should be declared volatile or an overly-ambitious optimizer might cause the program to loop infinitely.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What would be the best way to keep track of how many times the
I am trying to figure out the best way to keep my application responsive.
What is the best way to keep a List<String[]> while my application runs? I
In a Cocoa document-based application, what's the best way to keep the menu item
What's best way to keep user logged on a PHP-powered site until he closes
what is the best way to keep a child control up to date for
I am trying to found it the best way to keep my scripting i.e.
In PHP, what's the best way to copy an array and keep the keys
What's the best way to keep state in a bottom bar that sticks to
I was just thinking what is the best way to keep images in IPhone/iPad

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.