Solved

Detect X button click event on cmd window

Posted on 2010-09-03
3
432 Views
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?
0
Comment
Question by:gprodri
[X]
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
3 Comments
 
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?
0
 
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!");
                    break;

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

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

                case CtrlTypes.CTRL_LOGOFF_EVENT:
                case CtrlTypes.CTRL_SHUTDOWN_EVENT:
                    isclosing = true;
                    Console.WriteLine("User is logging off!");
                    break;
            }
            return true;
        }
        [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
        }

Open in new window

0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 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
        CTRL_BREAK_EVENT
        CTRL_CLOSE_EVENT
        CTRL_LOGOFF_EVENT = 5
        CTRL_SHUTDOWN_EVENT
    End Enum

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

            ' ...

            System.Threading.Thread.Sleep(100)
        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("")
                Console.WriteLine("Program being closed!")
                Console.WriteLine("")
                Console.WriteLine("Press Enter to continue...")
                Console.ReadLine()

            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

0

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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