• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

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?
0
dkilby
Asked:
dkilby
1 Solution
 
käµfm³d 👽Commented:
Try this:
using System.Diagnostics;

...

while (true)
{
    Process[] procs = Process.GetProcessesByName("procname");  // without ".exe"

    if (procs == null || procs.Length <= 0)
    {
        break;
    }

    System.Threading.Thread.Sleep(300000);  // Sleep at least 5 minutes
}

Open in new window

0
 
dkilbyAuthor Commented:
thank you
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now