简体   繁体   English

等待过程中的过程

[英]wait for process within process

Im trying to build a cmd.exe wrapper but i cant figure out how to wait for a process inside the cmd process to finish. 我试图建立一个cmd.exe包装器,但是我无法弄清楚如何等待cmd进程内部的进程完成。

When im run ping.exe in my program, my "inputline" (the path) will output to the console before the ping-command is finished. 当我在程序中运行ping.exe时,在ping命令完成之前,我的“ inputline”(路径)将输出到控制台。

I cant use: .WaitForInputIdle(); 我不能使用:.WaitForInputIdle(); because I don't have a GUI. 因为我没有GUI。 "This could be because the process does not have a graphical interface." “这可能是因为该过程没有图形界面。”

Is it even possible to solve or is there any better way to do it? 甚至有可能解决,还是有更好的方法呢?

在此处输入图片说明

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ObjectTest
{
    class cmd
    {
        Process cmdProcess;
        StreamWriter cmdStreamWriter;

        public cmd()
        {
            //WinAPI.ShowConsoleWindow();
            Process();
            //WinAPI.HideConsoleWindow();
        }

        private void Process()
        {
            StartCmd();
            string command = "";
            while (command != "exit")
            {

                Thread.Sleep(100);
                cmdProcess.WaitForInputIdle();
                Console.Write(Environment.NewLine + Directory.GetCurrentDirectory() + "> ");
                command = Console.ReadLine();
                SendCommand(command);
            }
        }


        private void StartCmd()
        {
            cmdProcess = new Process();

            cmdProcess.StartInfo.FileName = "cmd.exe";
            cmdProcess.StartInfo.UseShellExecute = false;
            cmdProcess.StartInfo.CreateNoWindow = true;
            cmdProcess.StartInfo.RedirectStandardOutput = true;
            cmdProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
            cmdProcess.ErrorDataReceived += new DataReceivedEventHandler(SortOutputHandler);
            cmdProcess.StartInfo.RedirectStandardInput = true;
            cmdProcess.Start();

            cmdStreamWriter = cmdProcess.StandardInput;
            cmdProcess.BeginOutputReadLine();
        }

        private void SendCommand(string command)
        {
            cmdStreamWriter.WriteLine(command);
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            cmdStreamWriter.Close();
            cmdProcess.WaitForExit();
            cmdProcess.Close();
        }

        private static void SortOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
        {
            if (!string.IsNullOrEmpty(outLine.Data))
            {
                Console.WriteLine(outLine.Data);
            }
        }


    }
}

If it's only actual processes you want to catch you can use polling methods as such: 如果您只是想捕获的实际进程,则可以使用以下轮询方法:

  1. Before running the command, collect all existing processes of, for example, ping (.exe), using Process.GetProcessesByName , and store their PIDs. 在运行命令之前,请使用Process.GetProcessesByName收集所有现有进程(例如ping (.exe))并存储其PID。

  2. Run the command 运行命令

  3. In order to wait for exit, Again scan for all ping processes, and wait on all processes that did not previously exist. 为了等待退出,再次扫描所有ping进程,然后等待以前不存在的所有进程。

Note that this will fail, or at least not be accurate, if another process will launch another ping between steps 1 and 3. 请注意,如果另一个进程将在步骤1和3之间启动另一个ping ,则此操作将失败,或者至少是不准确的。

You should also keep in mind that CMD does not wait on all type of processes, only those which are defined as console applications (see this question). 您还应该记住, CMD不会等待所有类型的进程,只会等待定义为控制台应用程序的那些进程(请参阅问题)。

When starting a process, you can specify that you want to wait: 启动过程时,可以指定要等待的时间:

var process = Process.Start(...);
process.WaitForExit();

Here's a link to MSDN explaining it: http://msdn.microsoft.com/en-us/library/fb4aw7b8(v=vs.110).aspx 这是解释MSDN的MSDN链接: http : //msdn.microsoft.com/zh-cn/library/fb4aw7b8(v=vs.110).aspx

I think this is wat you need. 我认为这是您需要的水。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM