Solved

Sending New Process Handle to Current Program

Posted on 2011-09-22
5
209 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

This is an article about Leadership and accepting and adapting to new challenges. It focuses mostly on upgrading to Windows 10.
Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 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

23 Experts available now in Live!

Get 1:1 Help Now