0

I have a started win-form application with some codes in FormClosing() & FormClosed() events.
With another application i want to forcibly close that first application like this :

    string procId = proc.Id.ToString();

    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = "taskkill.exe";
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.Arguments = "/f /t /pid " + procId;
    Process.Start(startInfo);

When i want for forcibly close first application by those codes or using with Task Manager -> End Process , FormClosing() & FormClosed() events are not fired.

How can i force those events to fire in every situation or what is the solution?

SilverLight
  • 19,668
  • 65
  • 192
  • 300
  • 2
    If your process is being killed, there's nothing you can do. _Why_ do you want that code to run? Can't you do a cleanup of a killed instance upon the next startup? – CodeCaster Sep 13 '15 at 12:45
  • No man, in startup and close i have different codes for writing some data in a file. i can't change them! – SilverLight Sep 13 '15 at 12:48
  • 2
    check this question : :http://stackoverflow.com/questions/474679/capture-console-exit-c-sharp – Ehsan Sajjad Sep 13 '15 at 12:50
  • Generally, either you wait until next startup, or you tell the process you are about to kill it and allow it to perform actions beforehand (assuming both processes are under your control). – SimpleVar Sep 13 '15 at 12:50
  • @Ehsan Sajjad that thread simulates Form_Closing() event in a ConsoleApplication and in winforms we have that event. Just need to run it in every termination. – SilverLight Sep 13 '15 at 13:00
  • @CodeCaster thanks for the answer, but didn't help. please forget startup codes and there is no need to clean up codes. my situation is different and in every close i should some values. that's all... – SilverLight Sep 13 '15 at 13:04
  • **You can't**. That's all. If your process is being killed, it is terminated on the spot. There is no code to run anymore. If you don't explain your situation, the answer is "You can't", which I gave. – CodeCaster Sep 13 '15 at 13:04
  • but there should be an event during ENDPROCESS!!!!!!!!! Or should a way to capture it. Imagine i want to have a full log file and write it this user forcibly closed app or some thing like that. – SilverLight Sep 13 '15 at 13:06
  • @MoonLight There shouldn't be, and there isn't such event. What are you trying to accomplish? – SimpleVar Sep 13 '15 at 13:08

1 Answers1

2

See Prevent C# app from process kill.

You cannot prevent your app's process being terminated, and you won't get an event it is being terminated.

So you must cater for the case where a process didn't clean up nicely at shutdown. For that matter, you need to do that anyway, what if the machine BSODs while your app is running? You can never assume the previous instance completed successfully.

It looks like your app closes some data file upon shutdown, so that the next time the program runs it can read the file again. If that is the case, you could write some "dirty flag" in or near the file and clean the file up on startup if it is flagged.

As explained in How does task manager kill my program? and MSDN: Terminating a Process (Windows):

Note that you cannot intercept or react upon TerminateProcess. Your process will die and there is nothing you can do before that happens.

If a process is terminated by TerminateProcess, all threads of the process are terminated immediately with no chance to run additional code. This means that the thread does not execute code in termination handler blocks.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272