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
Solved

Sending New Process Handle to Current Program

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

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

Suggested Solutions

Title # Comments Views Activity
Computer account cleanup 90 127
Where is this file? 3 25
SCCM 2012 R2, Rollback KB updates 4 18
need to convert function to c# 5 24
When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
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
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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.

808 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