Link to home
Start Free TrialLog in
Avatar of kvnsdr
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?


Avatar of sabeesh
sabeesh
Flag of United States of America image

You can you lock to lock first process

 lock (this)
            {
               
            }
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.
Avatar of kvnsdr
kvnsdr

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);
            }
}

SOLUTION
Avatar of mastoo
mastoo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kvnsdr

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(object sender, System.EventArgs e)
        {
                Process proc = new Process();
                proc.StartInfo.FileName = @"C:\Movefiles.exe";
                proc.Start();      
            }
            Application.Exit();
        }
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kvnsdr

ASKER

I was calling the method twice.

Once in the actual menuItemAppExit and then again calling menuItemAppExit in Form1_Leaving event.