System.Diaganostics.Process (when one process internally uses another)
Asked Answered
R

3

2

I've been using C# System.Diagnostics.Process to monitor the output of a command line utility.

The process I'm monitoring "internally" launches a second process, and as soon as it does, I receive no further output from the process object.

What's frustrating, is, if you execute the very same command (that I'm launching with the System.Diagnostics.Process object) with cmd.exe (manually), the console outputs every line I need to be seeing in my C# app!

However, if I (for testing purposes) launch cmd.exe with the System.Diagnostics.Process object, and run the command, it still stops outputting at the same point that it did previously (launching process1.exe directly); at the point the second.exe is utilised. I thought this test would consolidate all output from all processes involved, but it didn't. How can I get all this output into my C# application?

Racon answered 18/1, 2010 at 22:13 Comment(0)
E
5

The reason for this is that the System.Diagnostics.Process is literally only monitoring the process to which it is hooked.

One way to circumvent this problem would be for your first application to output when it is starting the second application, and when that output is received, monitor from your main application for the creation of the process from the (now third) application. Once the third application is started, it should appear in the System.Diagnostics.Process.GetProcesses() array, and you can then attach to it's OutputDataReceived event.

Your code would then look something like this (untested):

private void firstProcess_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
{
    if (e.Data == "Starting next process")
    {
        System.Diagnostics.Process newProcess = null;

        while (newProcess == null)
        {
            System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcesses();

            foreach (System.Diagnostics.Process proc in procs)
            {
                if (proc.ProcessName == "newProcess")
                {
                    newProcess = proc;
                    break;
                }
            }

            System.Threading.Thread.Sleep(100);
        }

        newProcess.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(newProcess_OutputDataReceived);
    }
}

void newProcess_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
{
    // Do something with your data received here.
}

Note that this is just a sample, and if your third process fails to start, or ends too quickly, then this method will hang in an infinite loop. This sample is simply meant to provide you with the knowledge to build something that works in your specific case, which I'm not totally familiar with. You should at a very minimum make sure that the while loop will not continue forever, and you'll probably want to make a few other adjustments as well.

EDIT: Alternately, if you can't modify the source to the first application, you could simply create a new thread that monitored in this manner (using the while loop) constantly and handled the output from the third process in a separate class, or simply re-routed the output from the third process into the handler for the output of the second process so that you could have a single method handling all of the output for both processes.

Eamon answered 18/1, 2010 at 22:35 Comment(3)
First of, I'm amazed at how quickly you've responded with such detail and I really appreciate your help. From the output of the first process, I'm able to determine the point at which the 2nd process "was" launched.Racon
To bad there isn't a object in the .Net framework that is as good of a "process output consolidator" as cmd.exe is (when run manually).Racon
Thanks for your efforts, but I still ran into this: #2096326Racon
M
1

Do you need to be launching cmd.exe at all? Can't you just initiate the process for rsync directly in your Process and then use something like the techniques described in this question to catch the output from the command execution so you can work with them in your code?

Maryannemarybella answered 18/1, 2010 at 22:25 Comment(1)
No, I only tried launching cmd.exe, instead, because I was hoping it would show all output from all processes. I initially tried it the way you are mentioning, and the output (using the techniques in the question you referenced) ceased after the first process started to utilise the second process that the first process calls internally.Racon
S
0

My first Googling was "ProcessDiagnosticInfo.GetForProcesses()"

So, I go to this link : https://github.com/microsoftarchive/msdn-code-gallery-microsoft/blob/master/OneCodeTeam/How%20to%20get%20information%20of%20running%20apps%20and%20processes%20in%20UWP%20apps/README.md

And DownLoad the GitHub file : https://github.com/microsoftarchive/msdn-code-gallery-microsoft

In OneCodeTeam/How to get information of running apps and processes in UWP apps foler, We can use that solution.

I had copied all codes to my new project and I can found similar symptom.

If you investigate Pakage.appxmanifest as code, you can find difference between original Github code and your code.

<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windo
ws10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap">

. . . .

<Capabilities>
    <Capability Name="internetClient" />
    <rescap:Capability Name="appDiagnostics"/>
</Capabilities>
Somersomers answered 11/11, 2022 at 3:34 Comment(1)
We don't usually write up our research path as it's really confusing for anybody who is not as advanced as the author of the answer. And I don't even know if your answer does anything for resolving the authors problem. Please add additional details for how to get things working and remove the research path part with edit. Thanks for your effort :)Gallinule

© 2022 - 2024 — McMap. All rights reserved.