18

How or where can I run a command when the application closes, even if is a debug stop?

I need to perform a command in any exit, even if the user is a developer and click on "stop debugging" button on Visual Studio.

I try with

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

but it doesn't work. Maybe I'm wrong or is not the event.

I'm using Winforms and not, on Form Close can't be the event.

I'm using Visual Studio 2005 Net Framework 2.0 (by client requirement), but is just for information.

Maybe can I rewrite this?:

public static void Exit();
Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
  • 2
    I'm curious - why would the client be bothered about debug being stopped? – m.edmondson Jun 13 '12 at 22:06
  • 3
    @m.edmondson I was looking at doing this for some web testing - it needs to fire up browsers during tests and exiting the debugger doesnt run the rest of a using block that cleans up. This is a bit annoying as you end up with lots of browsers being open – JonnyRaa Jan 27 '14 at 10:33

4 Answers4

15

The problem is the "stop debugging" function will halt the application entirely - so no more code within that application will run.

Your only way of achieving this would be to watch the process being debugged externally and execute the code if it has been halted.

According to [MSDN]:

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

However you may be able to achieve what you want with a visual studio add-in.

Jacob
  • 3,598
  • 4
  • 35
  • 56
m.edmondson
  • 30,382
  • 27
  • 123
  • 206
1

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?

Mausimo
  • 8,018
  • 12
  • 52
  • 70
0

You can use use Suspend and Resume layout. Also, if you have a combo box filled with stuff, it can slow you down. I would also consider going under "Solution Explorer" and right click and select "Properties" at the bottom. Choose "Configuration Properties" and select "Release" verse "Debugging." This will greatly improve form loading time. This link will explain

-1

Try this:

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("");
        }
    }
}

The reference is here (Fredrik Mörk): .NET Console Application Exit Event

Community
  • 1
  • 1
batressc
  • 1,499
  • 1
  • 18
  • 28
  • How can I call from CurrentDomain_ProcessExit a control on my Form1 ? – Leandro Bardelli Jun 13 '12 at 22:22
  • Want to launch a form from this event? – batressc Jun 13 '12 at 22:27
  • I need to change a lot of things, and one of them is a dispose of an object (notifyicon) on Form1 (created by designer) – Leandro Bardelli Jun 13 '12 at 22:29
  • If you need change any propertie of an object before to luch a form, in the constructor of the form, after of the method InitializeComponents(), you can write the necesary code for changing the properties of the objects. Explain me more what you do. – batressc Jun 13 '12 at 22:42
  • dispose the notifyicon or run a public void that dispose it when the app stops (also close a stream, etc etc etc). Clean up the solution and the build – Leandro Bardelli Jun 13 '12 at 22:46
  • Personally I think you should not close streams or dispose objects at this level of the program, each object must be dispose, in this case, to close the form ... but if you need to do this, you have to declare a static class and declare static variables (globals at application), if you need to force the release or closing streams, then put the code in that method. (Sorry my english, i can't speak english) – batressc Jun 14 '12 at 14:22
  • this doesnt seem to work for me. I am trying from a console app. – JonnyRaa Jan 27 '14 at 10:45
  • 16
    This absolutely does not work. The Process_Exit handler is not called when I press the stop debugging button. – Triynko Nov 24 '15 at 12:52