-2

Im trying to handle process exit (when user kill process from task manager) as below. But when I open the exe it opens multiple instances of same EXE.

    Process myProcess = null;
    String fileName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;

    myProcess.StartInfo.FileName = fileName;
    myProcess.StartInfo.CreateNoWindow = true;
    myProcess.EnableRaisingEvents = true;
    myProcess.Exited += new EventHandler(myProcess_Exited);
    myProcess.Start();
    Application.Run(new launchAuthentication());


private static void myProcess_Exited(object sender, System.EventArgs e)
{
    log.Info("myProcess_Exited");
}

How to prevent it?

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
syv
  • 3,528
  • 7
  • 35
  • 50
  • 3
    So whenever your process is run it starts another instance of itself, which then starts another instance of itself, which then... can you see where the problem is already? Did you just manually write a fork bomb? – Matti Virkkunen Feb 26 '16 at 20:39
  • Possible duplicate of [What is the correct way to create a single-instance application?](http://stackoverflow.com/questions/19147/what-is-the-correct-way-to-create-a-single-instance-application) – AXMIM Feb 26 '16 at 20:54
  • Also dupplicated is "[How to force C# .net app to run only one instance in Windows?](http://stackoverflow.com/questions/184084/how-to-force-c-sharp-net-app-to-run-only-one-instance-in-windows)" – AXMIM Feb 26 '16 at 20:54

1 Answers1

0

it's just a loop, you tell your process "start myProcess, then it starts myProcess myProcess, which starts myProcess .."