0

Hi my window form application have a dependency to file where it check the content and if the content is 0 then app should run otherwise it should message that instance is running fine. This is working fine when I am closing the app either by cross button form the form or form closed by its own (Environment.Exit(0);) but when I am closing the Window form application either by task manager or from the stop button of Visual studio ...the content of file is not changing to 0 . I have added the following code at:

private void Form1_FormClosing(Object sender, FormClosingEventArgs e)
{
    File.WriteAllText(filename2, "0");
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
    File.WriteAllText(filename2, "0");
}

And also before Environment.Exit(0);:

if (Status.Equals("Data Got Loaded"))
{
    File.WriteAllText(filename2, "0");
    aTimer.Stop();
    Environment.Exit(0);
}

Do I need to somewhere else so that application should write to the file before getting closed.

M. Schena
  • 2,039
  • 1
  • 21
  • 29
luckycse
  • 215
  • 1
  • 3
  • 13
  • [So basically you don't want to let users open your application while its running? use a mutex instead.](http://stackoverflow.com/a/229567/342740) – Prix Feb 02 '16 at 12:14
  • I tried using mutex but the problem with this is that if you are ruining your exe in multiple system then one instance in all the system will run in each system environment so that will be an issue. – luckycse Feb 04 '16 at 08:25
  • I see, so you want a cross-network mutex solution for your application? – Prix Feb 04 '16 at 12:50
  • yaa can you help me in that . – luckycse Feb 06 '16 at 03:57

1 Answers1

0

basically you can't tap into an event handler when your process is terminated from task manager. see similar post: C# Windows Form Killed By Task Manager...Is There a Way to Run Shutdown Function?

Community
  • 1
  • 1
bkdev
  • 432
  • 4
  • 9