C#

Нужно, чтобы когда закрывалось консольное приложение на C# происходило какое-то действие!

Как это реализовать. Помогите, очень нужно!
Задача неясна. Надо выполнять действия в конце твоей программы или при завершении какой-то другой программы?
Евгений Лынов
Евгений Лынов
36 282
Лучший ответ
Александр Босак Когда завершится моя программа.
Александр Босак Я не правильно сформулировал.
Программа должна что-то делать перед завершением (в конце свой работы). так.
Александр Босак Это свойство или отдельный код?
Александр Босак static void CheckClose()
{
while (true)
{
Process[] processList = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
if (processList.Length > 0)
{ }
else
{
Действие...
}
}
}
NAT LUENGNARUEMITCHAI
MY OTHER RECENT POSTS
Check whether the application is in DesignMode in WPF
WPF XBAP Application requires proxy authentication
WPF XBAP Application failed to work with ISS Proventia Buffer Overflow (vpatch.exe)
Handle leaks in svchost.exe
WPF ComboBox with LimitToList feature: Part I
WPF Nested Template Bug
new DateTime bug^H^H^Hby-design feature with Microsoft's latest patch for Daylight Saving
The screen saver does not work in Windows Vista when you use a Microsoft wireless mouse
32-bit Terminal Service Client bug in Vista 64-bit
.NET WebBrowser.Url and Uri Misuse
POST CATEGORIES
BizTalk
Singleton pattern
Perfmon
Throttling
Testing
routing failure
delivery notification
bug
Tracking
TDDS
Tracking job
ESB
Timeout
Property Bag
HTTP
side-by-side versionning
Loading property information list by namespace failed
Log Shipping
SSO
Access denied
Azure
Service Bus
Windows Azure SDK
ARCHIVES
December 2009 (1)
October 2009 (2)
June 2009 (2)
February 2009 (2)
October 2008 (1)
September 2007 (6)
August 2007 (1)
July 2007 (1)
February 2007 (1)
January 2007 (2)
October 2006 (1)
June 2006 (2)
May 2006 (3)
April 2006 (1)
March 2006 (1)
February 2006 (1)
December 2005 (1)
November 2005 (1)
October 2005 (5)
September 2005 (2)
August 2005 (5)
July 2005 (2)
June 2005 (4)
May 2005 (4)
April 2005 (16)
March 2005 (7)
February 2005 (3)
January 2005 (1)
December 2004 (5)
November 2004 (4)
October 2004 (7)
September 2004 (5)
Nat Luengnaruemitchai
Geek Blog
<< Can I ask for Complex Number Support in .NET? | Home | ASP.NET Vulnerability >>
Detecting Process Exit From Console Application in C#
Comments (20) | Share
There are mainly 2 types of Win32 applications, console application and window application. They have different way in handling application exit. To force Window application to exit, you need to send out WM_CLOSE message to the main window handle. That's pretty simple to handle. You can hook up to Application.ApplicationExit or Form.Close at the form level. However, in a console application, it is a little bit different. Console applications are somehow modeled after DOS console application where usually an application exits when stdin is dead or Ctrl+C is pressed. To handle this properly in C#, you can set a delegate to SetConsoleCtrlHandler.You will have an opportunity to clean up resource or finish your work before the application actually exits.
....
// Declare the SetConsoleCtrlHandler function
// as external and receiving a delegate.
[DllImport("Kernel32")]
public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);

// A delegate type to be used as the handler routine
// for SetConsoleCtrlHandler.
public delegate bool HandlerRoutine(CtrlTypes CtrlType);

// An enumerated type for the control messages
// sent to the handler routine.
public enum CtrlTypes
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT,
CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT
}

private static bool ConsoleCtrlCheck(CtrlTypes ctrlType)
{
// Put your own handler here
return true;
}

...

SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true);
Добавить необходимое действие перед строкой Environment.Exit(0);
это метод OnClose() или OnClosing()
Просто перед выходом из метода Main напиши то, что нужно.