Solved

windows forms + check if another program is running

Posted on 2011-03-09
3
318 Views
Last Modified: 2012-05-11
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
Comment
Question by:dkilby
3 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35086454
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
 

Author Closing Comment

by:dkilby
ID: 35086688
thank you
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35086774
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now