kvnsdr
asked on
Waiting for a program to close before opening another?
Q. How can I code a second program to wait for the first program (process) to close before opening?
I like to use a named mutex. Have your first app aquire a named mutex, and release it when closing. Then your second app can either poll the named mutex or just try to acquire it with an infinite timeout - in either case when the mutex is acquired the first app has closed. VStudio help has examples or I can give a link.
ASKER
I was thinking about using "Process" like so...
Here Form1 (via button event) calls Form2 to open.
Then within the Form2_Load, place this code:
[form2]
private void Form2_Load(object sender, EventArgs e)
{
Process[] main = Process.GetProcessesByName ("Form1"); // main program
if (main.Length == 0)
{
Process proc = new Process();
proc.StartInfo.FileName = @"C:\Movefiles.exe";
proc.Start();
}
else
{
Thread.Sleep(30000);
}
}
Here Form1 (via button event) calls Form2 to open.
Then within the Form2_Load, place this code:
[form2]
private void Form2_Load(object sender, EventArgs e)
{
Process[] main = Process.GetProcessesByName
if (main.Length == 0)
{
Process proc = new Process();
proc.StartInfo.FileName = @"C:\Movefiles.exe";
proc.Start();
}
else
{
Thread.Sleep(30000);
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Well, the Process method works great except two program windows open instead of just one.
NOTE: Only ONE program window opens if I manually double-click the Movefiles.exe with its directory.
Two windows open using the following code.
I call the Form2 from Form1 then exit like so:
[Form1]
private void menuItemAppExit_Click(obje ct sender, System.EventArgs e)
{
Process proc = new Process();
proc.StartInfo.FileName = @"C:\Movefiles.exe";
proc.Start();
}
Application.Exit();
}
NOTE: Only ONE program window opens if I manually double-click the Movefiles.exe with its directory.
Two windows open using the following code.
I call the Form2 from Form1 then exit like so:
[Form1]
private void menuItemAppExit_Click(obje
{
Process proc = new Process();
proc.StartInfo.FileName = @"C:\Movefiles.exe";
proc.Start();
}
Application.Exit();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I was calling the method twice.
Once in the actual menuItemAppExit and then again calling menuItemAppExit in Form1_Leaving event.
Once in the actual menuItemAppExit and then again calling menuItemAppExit in Form1_Leaving event.
lock (this)
{
}