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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 78k
  • Answers 78k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer It's up to the implementation of your NSOperation object to… May 11, 2026 at 3:40 pm
  • added an answer To elaborate on jdigital's answer: Since it's a hash algorithm… May 11, 2026 at 3:40 pm
  • added an answer Your Xaml is incorrect, from the looks of it. You… May 11, 2026 at 3:40 pm

Related Questions

I have a server application (singleton, simple .NET console application) that talks to a
Preamble So, this question has already been answered, but as it was my first
I've seen a lot of different takes on this subject so I figured if
I'm writing a program as follows: Find all files with the correct extension in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.