System.Diaganostics.Process(当一个进程在内部使用另一个进程时) [英] System.Diaganostics.Process (when one process internally uses another)

查看:198
本文介绍了System.Diaganostics.Process(当一个进程在内部使用另一个进程时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用C# System.Diagnostics.Process 来监视命令行实用程序的输出。



我在内部监控的进程会启动第二个进程,一旦进程完成,我就不会再从进程对象接收到任何输出。

令人沮丧的是,如果你使用cmd.exe(手动)执行相同的命令(我使用System.Diagnostics.Process对象启动),控制台输出每个



但是,如果我(用于测试目的)使用System.Diagnostics.Process对象启动cmd.exe,并运行该命令,它仍然在之前的同一点停止输出(直接启动process1.exe);在使用second.exe的点。我认为这个测试将巩固所有涉及的过程的所有输出,但它没有。这样做的原因是

System.Diagnostics.Process 实际上只监视它挂钩的进程。



解决这个问题的一种方法是在第一个应用程序启动第二个应用程序时输出,当接收到该输出时,从主应用程序监视从现在的第三个应用程序创建过程。一旦第三个应用程序启动,它应该出现在 System.Diagnostics.Process.GetProcesses()数组中,然后可以附加到它的 OutputDataReceived 事件。



您的代码将如下所示(未测试):

  private void firstProcess_OutputDataReceived(object sender,System.Diagnostics.DataReceivedEventArgs e)
{
if(e.Data ==Starting next process)
{
System.Diagnostics。进程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)
{
//在这里接收您的数据。
}

请注意,这只是一个示例,如果您的第三个进程无法启动,或结束得太快,则此方法将挂在无限循环中。这个例子只是为了给你提供知识来构建一个在你的特定情况下工作的东西,我不完全熟悉。您应该至少确保while循环不会永远继续,您可能还需要进行一些其他调整。



编辑: ,如果不能将源修改为第一个应用程序,则可以简单地创建一个以这种方式(使用while循环)监视的新线程,并在一个单独的类中处理来自第三个进程的输出,将第三个进程的输出路由到第二个进程的输出的处理程序中,以便可以有一个方法处理这两个进程的所有输出。


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?

解决方案

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.

这篇关于System.Diaganostics.Process(当一个进程在内部使用另一个进程时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆