Detect X button click event on cmd window

Posted on 2010-09-03
Last Modified: 2012-06-27
How do I detect from within a console application when the X button on its cmd window has been clicked?
Question by:gprodri
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 33601042
If you need that functionality then you should probably be using a WinForm instead...   =\

Can you give more details?
LVL 14

Expert Comment

by:Daniel Junges
ID: 33601049
SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCloseEvent), true);

private static bool ConsoleCloseEvent(CtrlTypes ctrlType)
            switch (ctrlType)
                case CtrlTypes.CTRL_C_EVENT:
                    isclosing = true;
                    Console.WriteLine("CTRL+C received!");

                case CtrlTypes.CTRL_BREAK_EVENT:
                    isclosing = true;
                    Console.WriteLine("CTRL+BREAK received!");

                case CtrlTypes.CTRL_CLOSE_EVENT:
                    isclosing = true;
                    Console.WriteLine("Program being closed!");

                case CtrlTypes.CTRL_LOGOFF_EVENT:
                case CtrlTypes.CTRL_SHUTDOWN_EVENT:
                    isclosing = true;
                    Console.WriteLine("User is logging off!");
            return true;
        public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);
        public delegate bool HandlerRoutine(CtrlTypes CtrlType);

        public enum CtrlTypes
            CTRL_C_EVENT = 0,
            CTRL_LOGOFF_EVENT = 5,

Open in new window

LVL 86

Accepted Solution

Mike Tomlinson earned 500 total points
ID: 33601104
*Not for's a VB implementation of junges's approach:
Imports System.Runtime.InteropServices
Module Module1

    Public isclosing As Boolean = False

    <DllImport("Kernel32")> _
    Public Function SetConsoleCtrlHandler(ByVal Handler As HandlerRoutine, ByVal Add As Boolean) As Boolean
    End Function

    Public Delegate Function HandlerRoutine(ByVal CtrlType As CtrlTypes) As Boolean

    Public Enum CtrlTypes
        CTRL_C_EVENT = 0
    End Enum

    Sub Main()
        SetConsoleCtrlHandler(New HandlerRoutine(AddressOf ConsoleCloseEvent), True)
        While Not isclosing

            ' ...

        End While
    End Sub

    Private Function ConsoleCloseEvent(ByVal ctrlType As CtrlTypes) As Boolean
        Select Case ctrlType
            Case CtrlTypes.CTRL_C_EVENT
                Console.WriteLine("CTRL+C received!")

            Case CtrlTypes.CTRL_BREAK_EVENT
                Console.WriteLine("CTRL+BREAK received!")

            Case CtrlTypes.CTRL_CLOSE_EVENT
                Console.WriteLine("Program being closed!")
                Console.WriteLine("Press Enter to continue...")

            Case CtrlTypes.CTRL_LOGOFF_EVENT, CtrlTypes.CTRL_SHUTDOWN_EVENT
                Console.WriteLine("User is logging off!")

        End Select

        isclosing = True
        Return True
    End Function

End Module

Open in new window


Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question