Process.start: how to get the output?

后端 未结 9 1412
無奈伤痛
無奈伤痛 2020-11-21 23:56

I would like to run an external command line program from my Mono/.NET app. For example, I would like to run mencoder. Is it possible:

  1. To get
9条回答
  •  不知归路
    2020-11-22 00:39

    you can use shared memory for the 2 processes to communicate through, check out MemoryMappedFile

    you'll mainly create a memory mapped file mmf in the parent process using "using" statement then create the second process till it terminates and let it write the result to the mmf using BinaryWriter, then read the result from the mmf using the parent process, you can also pass the mmf name using command line arguments or hard code it.

    make sure when using the mapped file in the parent process that you make the child process write the result to the mapped file before the mapped file is released in the parent process

    Example: parent process

        private static void Main(string[] args)
        {
            using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew("memfile", 128))
            {
                using (MemoryMappedViewStream stream = mmf.CreateViewStream())
                {
                    BinaryWriter writer = new BinaryWriter(stream);
                    writer.Write(512);
                }
    
                Console.WriteLine("Starting the child process");
                // Command line args are separated by a space
                Process p = Process.Start("ChildProcess.exe", "memfile");
    
                Console.WriteLine("Waiting child to die");
    
                p.WaitForExit();
                Console.WriteLine("Child died");
    
                using (MemoryMappedViewStream stream = mmf.CreateViewStream())
                {
                    BinaryReader reader = new BinaryReader(stream);
                    Console.WriteLine("Result:" + reader.ReadInt32());
                }
            }
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
    

    Child process

        private static void Main(string[] args)
        {
            Console.WriteLine("Child process started");
            string mmfName = args[0];
    
            using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting(mmfName))
            {
                int readValue;
                using (MemoryMappedViewStream stream = mmf.CreateViewStream())
                {
                    BinaryReader reader = new BinaryReader(stream);
                    Console.WriteLine("child reading: " + (readValue = reader.ReadInt32()));
                }
                using (MemoryMappedViewStream input = mmf.CreateViewStream())
                {
                    BinaryWriter writer = new BinaryWriter(input);
                    writer.Write(readValue * 2);
                }
            }
    
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
    

    to use this sample, you'll need to create a solution with 2 projects inside, then you take the build result of the child process from %childDir%/bin/debug and copy it to %parentDirectory%/bin/debug then run the parent project

    childDir and parentDirectory are the folder names of your projects on the pc good luck :)

提交回复
热议问题