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 126175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:16:33+00:00 2026-05-11T05:16:33+00:00

I know that I have to use SetConsoleCtrlHandler() if I want to manage console

  • 0

I know that I have to use SetConsoleCtrlHandler() if I want to manage console closing events.

I do not know how to block the CTRL_CLOSE_EVENT. I’ve tried returning false/true if it catches that event, but no success

Here is what I have so far (thank you Anton Gogolev!):

[DllImport("Kernel32")] public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);  public delegate bool HandlerRoutine(CtrlTypes CtrlType);  public enum CtrlTypes{     CTRL_C_EVENT = 0,     CTRL_BREAK_EVENT,     CTRL_CLOSE_EVENT,     CTRL_LOGOFF_EVENT = 5,     CTRL_SHUTDOWN_EVENT }  private static bool ConsoleCtrlCheck(CtrlTypes ctrlType) {      if(ctrlType == CtrlTypes.CTRL_CLOSE_EVENT)         return false;// I have tried true and false and viceversa with the return                         // true/false but I cant seem to get it right.     return true; }   //and then I use this to call it SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true); 

Also, is it possible to run a new thread to monitor if the console is closing and block that close if the main thread is in the middle of doing something?

  • 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-11T05:16:34+00:00Added an answer on May 11, 2026 at 5:16 am

    The documentation for SetConsoleCtrlHandler() says:

    The system generates CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, and CTRL_SHUTDOWN_EVENT signals when the user closes the console, logs off, or shuts down the system so that the process has an opportunity to clean up before termination.

    This implies that unlike when handling CTRL+C or CTRL+BREAK events, your process does not get the opportunity to cancel the close, logoff, or shutdown.

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

Sidebar

Related Questions

I know that I have to use: rdtsc. The measured function is deterministic but
Now that I know C++ I want to get into desktop application that have
I want to generate random numbers manually. I know that every language have the
I know that primary keys based on Guids do not have the best performance
I know that I can't use this: myView.frame.origin.x = 25.0; and that I have
Yes, I know that in ASP.NET MVC you have to use ViewModels. But I'm
I know that I have to avoid to use IN clause, but I don't
Does anyone know how sites that have a real-time feed of a lot of
I know that PHP doesn't yet have native Enumerations. But I have become accustomed
I know that the asp.net repeater doesnt have a Client side object model, and

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.