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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:40:04+00:00 2026-06-13T18:40:04+00:00

I made a sample to check what happen when Ctrl+C is pressed in windows

  • 0

I made a sample to check what happen when Ctrl+C is pressed in windows console application:

bool    TerminationFlag=true;

int main()
{
    g_hTerminateEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
    ::SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
    while(1)
    {
        if(TerminationFlag == false)
        {
            break;
        }
    }
    return 0;
}

BOOL WINAPI ConsoleCtrlHandler(DWORD dwCtrlType)
{

    if (dwCtrlType == CTRL_C_EVENT ||
        dwCtrlType == CTRL_BREAK_EVENT ||
        dwCtrlType == CTRL_CLOSE_EVENT)
    {
        TerminationFlag=false;
        ::SetEvent(g_hTerminateEvent);
        return TRUE;
    }
    return FALSE;
}

I tested the code by running it using start debugging option in visual
studio when I press ctrl+c I get the following message

First-chance exception at 0x7c87647d

when I press on continue option my code comes to the line TerminationFlag=false; even though I have handled Ctrl+C in control handler. Can you please tell me whats the problem?

  • 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. Editorial Team
    Editorial Team
    2026-06-13T18:40:05+00:00Added an answer on June 13, 2026 at 6:40 pm

    I assume you’re using Microsoft Visual Studio from your description of the problem. The first chance exception being raised is the CTRL-C event which is trapped by the debugging environment. This is expected behaviour.

    You can choose to ignore this: go to Debug menu/Exceptions/Win32 Exceptions and take out the CONTROL-C check from the “Thrown” column menu. This will ensure that the debugger only breaks on CONTROL-C when it is user-unhandled. See picture below:

    enter image description here

    Incidentally, you should be waiting for the termination event not polling for a flag. You may want something like:

    #include "windows.h"
    #include <iostream>
    HANDLE g_hTerminateEvent;
    
    BOOL WINAPI ConsoleCtrlHandler(DWORD dwCtrlType)
    {
    
        if (dwCtrlType == CTRL_C_EVENT ||
            dwCtrlType == CTRL_BREAK_EVENT ||
            dwCtrlType == CTRL_CLOSE_EVENT)
        {
            std::cout << "Terminating" << std::endl;
            SetEvent(g_hTerminateEvent);
    
            return TRUE;
        }
        return FALSE;
    }
    int main()
    {
        g_hTerminateEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
        SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
    
        DWORD result = WaitForSingleObject(g_hTerminateEvent, INFINITE);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made a sample application using windows azure dedicated caching ( preview ). The
I have made a sample application which constructs a filter graph to capture audio
I have made a sample for posting on Facebook using the basic Facebook library
I am trying to read this file I created as sample made up from
UPDATE: I made major changes to this post - check the revision history for
In my .NET 2.0 project, I made an empty derived class of System.Windows.Forms.PictureBox :
I have made a simple installer application in Delphi, nothing fancy. Basically I include
i want move two or more sticky windows when i move a main window
I'm trying to compile the Open Kinect drivers for Python on Windows I made
In a web application if a user check the Remember Me box I'm gonna

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.