Is there a way to force C# compiler, visual studio 2010, to call a method whenever the user closes the console ? I know that this can be done in windows forms by some sort of event handling or using dispose methods, but what about console, is there any similar way to be able to call a method when closing it ?
Asked
Active
Viewed 1,658 times
6
-
3The compiler can't do anything about... when your program is running, the compiler's work is already done. – Thomas Levesque Jun 13 '12 at 12:18
-
2See [here](http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/707e9ae1-a53f-4918-8ac4-62a1eddb3c4a/) – Bali C Jun 13 '12 at 12:23
-
@BaliC's link actually seems to work. – vgru Jun 13 '12 at 13:03
-
possible duplicate of ["On Exit" for a Console Application](http://stackoverflow.com/questions/4646827/on-exit-for-a-console-application) – Hans Passant Jun 13 '12 at 13:26
3 Answers
1
You don't need a special technique in most cases...just add the routine you were thinking to the end of the program; well you can write your program in such a manner. However, if you still need to handle it there is a Ctrl+C mechanism or what is called CancekKeyPress event triggered.
Check this
0
No ! that is not possible because you did not write code for the console window and you dont have any events to override

Shyju
- 214,206
- 104
- 411
- 497
-2
Use assembly language programming to do that.
To use assembly language construct use _asm {} in c# //google it further
To stop someone from closing the console window override the interrupt that is called on ctrl+c or there is another interrupt that is called on program exit. Override it ..

sp3tsnaz
- 506
- 7
- 16