2

I got two executables (Program1.exe and Program2.exe) which each open the other one when closed:

Here some code from Program1.exe:

private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
   Application.Exit();
   Process.Start(Environment.CurrentDirectory + @"\Program2.exe");
}

However, when I do this, it opens two windows instead of one. Is there any way to let it open one window only?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
jacobz
  • 3,191
  • 12
  • 37
  • 61

2 Answers2

2

Application.Exit will call FormClosing event one more time. So Process.Start is called twice and thus it opens twice.

Move the following code in FormClosed event

private void Main_FormClosed(object sender, FormClosedEventArgs e)
{
    Process.Start(Environment.CurrentDirectory + @"\Program2.exe");
}
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
2

Try removing Application.Exit();.

It calls Main_FormClosing

Jerry
  • 4,258
  • 3
  • 31
  • 58