C# How to capture console window close

Sudip Bhatt 2,271 Reputation points

I found few write up for this topic

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

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

    A way is with SetConsoleCtrlHandler

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

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