Solved

Sending New Process Handle to Current Program

Posted on 2011-09-22
5
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 86

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 86

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

Building an interactive eFuture classroom

Watch and learn how ATEN provided a total control system solution including seamless switching matrix switch, HDBaseT extenders, PDU, lighting control to build an interactive eFuture classroom.

Question has a verified solution.

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

How to record audio from input sources to your PC – connected devices, connected preamp to record vinyl discs, streaming media, that play through your audio card: Vista, Windows 7, Windows 8, Windows 8.1 and Windows 10 – both 32 bit & 64.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

732 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