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

两盒软妹~` 提交于 2019-12-04 17:32:52

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.

glenatron

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!