Solved

Sending New Process Handle to Current Program

Posted on 2011-09-22
5
208 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup". After a while, you have entered a loop for Auto repair which does not fix anything and you will be in a  panic as all your work w…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

762 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

20 Experts available now in Live!

Get 1:1 Help Now