C# How to capture console window close

Sudip Bhatt 2,271 Reputation points
2021-01-29T15:53:09.29+00:00

I found few write up for this topic
https://stackoverflow.com/questions/4646827/on-exit-for-a-console-application
https://social.msdn.microsoft.com/Forums/vstudio/en-US/3295e38d-2267-425a-a7f8-9e6035331e53/how-to-capture-closing-of-c-console-apps?forum=csharpgeneral

I want when click on cross button of console window then i want to capture this and fire a mail.
if some one close or kill my console application from TaskBar then also i want to capture this and fire a mail.

How it will be possible? specially i have to capture and fire email when any user kill application from Task Bar window.

Please help me with idea and sample code. thanks

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,390 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 82,116 Reputation points
    2021-01-29T16:46:59.267+00:00

    A way is with SetConsoleCtrlHandler

    A test (I added different Beeps on Exit... ) =>

    I cannot post code again, so a link.. : SetConsoleCtrlHandler Test