Solved

windows forms + check if another program is running

Posted on 2011-03-09
3
319 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 75

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

26 Experts available now in Live!

Get 1:1 Help Now