Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Detect X button click event on cmd window

Posted on 2010-09-03
Medium Priority
Last Modified: 2012-06-27
How do I detect from within a vb.net 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 2000 total points
ID: 33601104
*Not for points...here'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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

610 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