Stop debugging event in C #

How and where can I run the command when the application closes, even if it's debugging?

I need to execute the command in any output, even if the user is a developer and clicks the “stop debugging” button in Visual Studio.

I'm trying with

Application.ApplicationExit += new EventHandler(this.OnApplicationExit); 

but that will not work. Maybe I'm wrong or not an event.

I use Winforms, and not, in Form Close there cannot be an event.

I am using Visual Studio 2005 Net Framework 2.0 (as requested by the client), but for information only.

Can I rewrite this ?:

 public static void Exit(); 
+6
source share
3 answers

The problem is that the "stop debugging" function will completely stop the application, so there will be no more in this application.

The only way to achieve this is to observe how the process is being debugged externally and executing code if it was stopped.

According to [MSDN]:

Stop Debugging terminates the process that you are debugging if the program was launched from Visual Studio.

However, you can achieve what you want with a visual studio add-in .

+11
source

Try the following:

 using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace Prueba { static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); //This is the magic line code :P AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit); } static void CurrentDomain_ProcessExit(object sender, EventArgs e) { MessageBox.Show(""); } } } 

Link here (Fredrik Mörk): .NET Console Application Exit Event

+1
source

According to MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.applicationexit%28v=vs.80%29.aspx

 // Handle the ApplicationExit event to know when the application is exiting. Application.ApplicationExit += new EventHandler(this.OnApplicationExit); private void OnApplicationExit(object sender, EventArgs e) { // When the application is exiting } 

Is this what you implemented?

0
source

Source: https://habr.com/ru/post/918036/


All Articles