0

I'm using VS 2010, winForm.

Can someone suggest how to run method when program process is ended?

I tried: How to run code before program exit? solution, but it only works is I close form by "close button" if I end process in task-manager it is not called, is it possible to handle such event?

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new MainForm());
    AppDomain.CurrentDomain.ProcessExit += new EventHandler (OnProcessExit); 
}

public static void OnProcessExit(object sender, EventArgs e)
{
    if (Utils.RemindTime != DateTime.MinValue)
        Utils.SetStartup(true);
}
Community
  • 1
  • 1
Edgar
  • 1,120
  • 4
  • 28
  • 53
  • If you kill the process, you kill the process. There is nothing left that can perform any tasks. – J. Steen Dec 06 '12 at 17:52
  • See [this question](http://stackoverflow.com/questions/4479707/guarantee-code-execution-even-on-process-kill) for a detailed answer. In short: it cannot be done – Scott Miller Dec 06 '12 at 17:53
  • Can you put the code directly under the `Application.Run` line, instead of setting an event handler? If that doesn't work, try putting the event handler code before `Application.Run`. – Matthew Dec 06 '12 at 17:53
  • @Matthew That only works if the process is torn down nicely, not when it's forcibly killed by the OS. – Servy Dec 06 '12 at 18:21

1 Answers1

1

You use another program to start this program. Let's say your application is "notepad":

class Program
{
    static void Main(string[] args)
    {
        var startInfo = new ProcessStartInfo("notepad.exe");
        var process = Process.Start(startInfo);
        process.WaitForExit();
        //Do whatever you need to do here
        Console.Write("Notepad Closed");
        Console.ReadLine();
    }
}

You could to something similar with batch files on Windows or shell scripts in Linux.

Of course it doesn't give you access to the internals of the program you launched, but it's the only way you can make sure "some code" is executed after the process is killed by the task manager

TimothyP
  • 21,178
  • 26
  • 94
  • 142
  • i see, thnx for help, probably I should think out something different :))) – Edgar Dec 06 '12 at 18:16
  • 1
    Of course, if someone doesn't kill this process before "notepad" – L.B Dec 06 '12 at 18:23
  • I'm not sure what you are trying to do exactly, but maybe at one point you want to figure out if your app was shutdown correctly or incorrectly (by killing it). What you do is you create a file when the app starts, and when it is shutdown correctly, you delete the file. In the example above you check if that file exists, if it does, the app was killed, if not the app was shutdown properly – TimothyP Dec 06 '12 at 18:24
  • @L.B. indeed :p There are always ways around it :p So one would also need to have basic security in place. then again, physical access to a machine = full access. Hell, one could modify an application that is in memory realtime without the user even knowing... but +1 though, because it's true 100% – TimothyP Dec 06 '12 at 18:26