I was wondering how to log a process/application start/exit in event viewer? I know services are logged but applications are not. I need to run a program (or a batch file) after another program/process has been closed. I = i + 1 listBox1.Items.Add(i) End While ' The user wants to exit the application. Close everything down. Application.Exit End Sub Remarks. The Exit method stops all running message loops on all threads and closes all windows of the application. This method does not necessarily force the application to exit.
- Nov 18, 2011 Remarks. You can detect when an application shuts down by handling the Exit event. This allows your application to perform common application shutdown tasks, including saving data for the next application session and logging.
- The Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. If you have validation code in either of these events that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.
- Oct 02, 2009 We know Word Object model provides an event handler called ApplicationQuit, which will be triggered when the application quits, but we don’t have any such events for Excel (even for PowerPoint and some other Office applications). In order to workaround this issue, first thing that will come in our mind is to use an Office.
- Hi Eby, I think you have misunderstood the SetConsoleCtrlHandler’s feature, it is detecting that the caller that invoke the SetConsoleCtrlHandler, in other words, when I run the my code above in the console window, I press the “Ctrl + C”, and then it will detect it and exit, it is just a self-detect program, there are no server and client, it can be used in only one console application.
Active1 year, 8 months ago
In .NET, is there a method, such as an event, for detecting when a Console Application is exiting? I need to clean up some threads and COM objects.
I am running a message loop, without a form, from the console application. A DCOM component that I am using seems to require that the application pump messages.
I have tried adding a handler to Process.GetCurrentProcess.Exited and Process.GetCurrentProcess.Disposed.
I have also tried adding a handler to Application.ApplicationExit and Application.ThreadExit events, but they are not firing. Perhaps that is because I am not using a form.
user79755user797551,40844 gold badges2121 silver badges3636 bronze badges
6 Answers
You can use the
ProcessExit
event of the AppDomain
:Update
Here is a full example program with an empty 'message pump' running on a separate thread, that allows the user to input a quit command in the console to close down the application gracefully. After the loop in MessagePump you will probably want to clean up resources used by the thread in a nice manner. It's better to do that there than in ProcessExit for several reasons:
- Avoid cross-threading problems; if external COM objects were created on the MessagePump thread, it's easier to deal with them there.
- There is a time limit on ProcessExit (3 seconds by default), so if cleaning up is time consuming, it may fail if pefromed within that event handler.
Here is the code:
Fredrik MörkFredrik Mörk136k2323 gold badges256256 silver badges319319 bronze badges
Here is a complete, very simple .Net solution that works in all versions of windows. Simply paste it into a new project, run it and try CTRL-C to view how it handles it:
JJ_Coder4HireJJ_Coder4Hire
The application is a server which simply runs until the system shuts down or it receives a Ctrl+C or the console window is closed.
Due to the extraordinary nature of the application, it is not feasible to 'gracefully' exit. (It may be that I could code another application which would send a 'server shutdown' message but that would be overkill for one application and still insufficient for certain circumstances like when the server (Actual OS) is actually shutting down.)
Because of these circumstances I added a 'ConsoleCtrlHandler' where I stop my threads and clean up my COM objects etc...
This setup seems to work out perfectly. Here is a link to some C# code for the same thing.
user79755user79755
1,40844 gold badges2121 silver badges3636 bronze badges
EricLawEricLaw51.9k55 gold badges127127 silver badges173173 bronze badges
If you are using a console application and you are pumping messages, can't you use the WM_QUIT message?
Mike DinescuMike Dinescu41.4k88 gold badges8989 silver badges130130 bronze badges
As a good example may be worth it to navigate to this project and see how to handle exiting processes grammatically or in this snippet from VM found in here
stenlystenly