コード例 #1
0
        private static async Task RedirectionToFileAsync()
        {
            var tempFile = Path.GetTempFileName();

            var si = new ChildProcessStartInfo("env")
            {
                ExtraEnvironmentVariables = new Dictionary <string, string> {
                    { "A", "A" }
                },
                StdOutputRedirection = OutputRedirection.File,
                StdErrorRedirection  = OutputRedirection.File,
                StdOutputFile        = tempFile,
                StdErrorFile         = tempFile,
            };

            using (var p = ChildProcess.Start(si))
            {
                await p.WaitForExitAsync();
            }

            // LANG=C.UTF-8
            // ...
            Console.WriteLine(File.ReadAllText(tempFile));
            File.Delete(tempFile);
        }
コード例 #2
0
        private static async Task RedirectionToFileAsync()
        {
            var tempFile = Path.GetTempFileName();

            var si = new ChildProcessStartInfo("cmd", "/C", "set")
            {
                ExtraEnvironmentVariables = new Dictionary <string, string> {
                    { "A", "A" }
                },
                StdOutputRedirection = OutputRedirection.File,
                StdErrorRedirection  = OutputRedirection.File,
                StdOutputFile        = tempFile,
                StdErrorFile         = tempFile,
                Flags    = ChildProcessFlags.UseCustomCodePage,
                CodePage = Encoding.Default.CodePage, // UTF-8 on .NET Core
            };

            using (var p = ChildProcess.Start(si))
            {
                await p.WaitForExitAsync();
            }

            // A=A
            // ALLUSERSPROFILE=C:\ProgramData
            // ...
            Console.WriteLine(File.ReadAllText(tempFile));
            File.Delete(tempFile);
        }
コード例 #3
0
            static async Task SpawnCmdAsync()
            {
                var si = new ChildProcessStartInfo("sleep", "3s");

                using var p = ChildProcess.Start(si);
                await p.WaitForExitAsync();
            }
コード例 #4
0
        // Truely asynchronous WaitForExitAsync: WaitForExitAsync does not consume a thread-pool thread.
        // You will not need a dedicated thread for handling a child process.
        // You can handle more processes than the number of threads.
        private static async Task WaitForExitAsync()
        {
            const int N = 128;

            var stopWatch = Stopwatch.StartNew();
            var tasks     = new Task[N];

            for (int i = 0; i < N; i++)
            {
                tasks[i] = SpawnCmdAsync();
            }

            // Spawned 128 processes.
            // The 128 processes have exited.
            // Elapsed Time: 3367 ms
            Console.WriteLine("Spawned {0} processes.", N);
            await Task.WhenAll(tasks);

            Console.WriteLine("The {0} processes have exited.", N);
            Console.WriteLine("Elapsed Time: {0} ms", stopWatch.ElapsedMilliseconds);

            async Task SpawnCmdAsync()
            {
                var si = new ChildProcessStartInfo("cmd", "/C", "timeout", "3")
                {
                    StdInputRedirection  = InputRedirection.ParentInput,
                    StdOutputRedirection = OutputRedirection.NullDevice,
                };

                using (var p = ChildProcess.Start(si))
                {
                    await p.WaitForExitAsync();
                }
            }
        }
コード例 #5
0
            static async Task SpawnCmdAsync()
            {
                var si = new ChildProcessStartInfo("sleep", "3s")
                {
                    StdInputRedirection  = InputRedirection.ParentInput,
                    StdOutputRedirection = OutputRedirection.NullDevice,
                };

                using var p = ChildProcess.Start(si);
                await p.WaitForExitAsync();
            }
コード例 #6
0
            static async Task SpawnCmdAsync(int i)
            {
                var si = new ChildProcessStartInfo("waitfor", "/T", "3", $"pause{i}")
                {
                    StdInputRedirection  = InputRedirection.ParentInput,
                    StdOutputRedirection = OutputRedirection.NullDevice,
                    Flags = ChildProcessFlags.AttachToCurrentConsole,
                };

                using var p = ChildProcess.Start(si);
                await p.WaitForExitAsync();
            }
コード例 #7
0
        private static async Task RedirectionToFileAsync()
        {
            var si = new ChildProcessStartInfo("cmd", "/C", "set")
            {
                StdOutputRedirection = OutputRedirection.File,
                StdOutputFile        = "env.txt",
            };

            using (var p = ChildProcess.Start(si))
            {
                await p.WaitForExitAsync();
            }

            // ALLUSERSPROFILE=C:\ProgramData
            // ...
            Console.WriteLine(File.ReadAllText("env.txt"));
        }
コード例 #8
0
        private static async Task BasicAsync()
        {
            var si = new ChildProcessStartInfo("cmd", "/C", "echo", "foo")
            {
                StdOutputRedirection = OutputRedirection.OutputPipe,
            };

            using var p = ChildProcess.Start(si);
            using (var sr = new StreamReader(p.StandardOutput !))
            {
                // "foo"
                Console.Write(await sr.ReadToEndAsync());
            }
            await p.WaitForExitAsync();

            // ExitCode: 0
            Console.WriteLine("ExitCode: {0}", p.ExitCode);
        }
コード例 #9
0
        private static async Task RedirectionToFileAsync()
        {
            var tempFile = Path.GetTempFileName();

            var si = new ChildProcessStartInfo("ls", "/")
            {
                StdOutputRedirection = OutputRedirection.File,
                StdOutputFile        = tempFile,
            };

            using (var p = ChildProcess.Start(si))
            {
                await p.WaitForExitAsync();
            }

            // bin
            // boot
            // ...
            Console.WriteLine(File.ReadAllText(tempFile));
            File.Delete(tempFile);
        }
コード例 #10
0
        // Truely asynchronous WaitForExitAsync: WaitForExitAsync does not consume a thread-pool thread.
        // You will not need a dedicated thread for handling a child process.
        // You can handle more processes than the number of threads.
        private static async Task WaitForExitAsync()
        {
            const int N = 128;

            var stopWatch = Stopwatch.StartNew();
            var tasks     = new Task[N];

            for (int i = 0; i < N; i++)
            {
                tasks[i] = SpawnCmdAsync();
            }

            // Spawned 128 processes.
            // The 128 processes have exited.
            // Elapsed Time: 3367 ms
            Console.WriteLine("Spawned {0} processes.", N);
            await Task.WhenAll(tasks);

            Console.WriteLine("The {0} processes have exited.", N);
            Console.WriteLine("Elapsed Time: {0} ms", stopWatch.ElapsedMilliseconds);

            async Task SpawnCmdAsync()
            {
                // Using timeout because Windows does not have a handy sleep command.
                // (`powershell -NoProfile -Command Sleep 3` is not an option because
                // it takes ~200 ms to boot.)
                //
                // This is super racy because these N timeout processes simultaneously
                // attempts to perform PeekConsoleInput followed by ReadConsoleInput.
                // When a console input arrives, some processes will get stuck in ReadConsoleInput.
                var si = new ChildProcessStartInfo("timeout", "3")
                {
                    StdInputRedirection  = InputRedirection.ParentInput,
                    StdOutputRedirection = OutputRedirection.NullDevice,
                };

                using var p = ChildProcess.Start(si);
                await p.WaitForExitAsync();
            }
        }