Solved

Sending New Process Handle to Current Program

Posted on 2011-09-22
5
213 Views
Last Modified: 2012-05-12
Hello all.

I am building a simple console launcher program that starts my Windows Forms application using System.Diagnostics.Process.Start;

I would like to pass that Windows Forms a handle to my launcher so that the Windows Forms app can check whether the launcher is still running and if so, to shut it down.

How can I go about doing this?
0
Comment
Question by:Dragonseer
5 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36582531
Why would your console launcher still be running?  If you control the code for the Console app then just exit out of it after you launch the WinForms app...
0
 

Author Comment

by:Dragonseer
ID: 36582544
I need to be able to do some operations on the launcher files. The reason I'm checking is so that in case there is an issue and the launcher doesn't close, my Win Form app won't try to access files that are still open.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36582605
Just use the Process Class to see if the Console app is running:
string ConsoleFileName = "ConsoleApplication1";
            Process[] ps = Process.GetProcessesByName(ConsoleFileName);
            if (ps.Length > 0)
            {
                MessageBox.Show("It's running!");
            }
            else
            {
                // do something else...
            }

Open in new window

0
 
LVL 4

Expert Comment

by:theHollow
ID: 36583529
Hello
If you are in control of the "System.Diagnostics.Process.Start", then you also have the PIDs.

I would first create a list of processes:

processes = new List<Process>();

Open in new window

When an application is started, put the starting process to the processes list.

using (Process process = Process.Start("SomeKindOfApp.exe"))
{
     process.EnableRaisingEvents = true;
     process.Exited += new EventHandler(process_Exited);
     processes.Add(process);
 }

Open in new window

I also set the "EnableRaisingEvent" property to "true" and attach to the "Exited" event.
That way, I will be notified whenever the started application exits (and the exit code).

My Exited event method would look like this:

private void process_Exited(object sender, EventArgs e)
{
    Process process = null;

    process = sender as Process;
    processes.Remove(process);
    process = null;
}

Open in new window

I remove the exiting process from the processes list.
You will always have all your started processes in the processes list, also containing the PID (Process ID).

You can present the processes list to a GridView or something like that. That way, you can at any time kill any of the processes.

If this program will not run at all times, you can always save the PID to an XMl file and loop through it when the program starts and check if they are still running.
You have the "Process.GetProcessById(<pid>)" and "Process.GetProcessByName(<pname>)" for that.

Lots of possibilities here.
Hope it helps :-)
0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 500 total points
ID: 36586109
Your console application can create a named mutex and your form application can check if that named mutex already exists. When your console application\launcher exits the mutex won't exist any longer indicating the launcher isn't running.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
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.

685 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