Link to home
Start Free TrialLog in
Avatar of dkilby
dkilbyFlag for Canada

asked on

windows forms + check if another program is running

Inside a windows form is there anyway of checking to see if another program is currently running, and if it is wait 5 mins or so and then check again, and keep checking until the other application has been closed, then once closed continuing on with rest of code?
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
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 dkilby

ASKER

thank you
How about a blocking approach?
Process[] ps = Process.GetProcessesByName("notepad");
            if (ps.Length > 0)
            {
                ps[0].WaitForExit();
                MessageBox.Show("It was closed...");
            }

Open in new window


Or an event based approach?  
private void Foo()
        {
            Process[] ps = Process.GetProcessesByName("notepad");
            if (ps.Length > 0)
            {
                ps[0].EnableRaisingEvents = true;
                ps[0].Exited += new EventHandler(process_Exited);
            }
        }

        void process_Exited(object sender, EventArgs e)
        {
            MessageBox.Show("It was closed...");
        }

Open in new window