Esempio n. 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);
        }
Esempio n. 2
0
        private async Task InstallForgeInOldWay(string installerPath)
        {
            var cp = new ChildProcess(BmclCore.Config.Javaw, new[] { "-jar", installerPath });

            cp.Start();
            await cp.WaitForExitAsync();
        }
Esempio n. 3
0
            static async Task SpawnCmdAsync()
            {
                var si = new ChildProcessStartInfo("sleep", "3s");

                using var p = ChildProcess.Start(si);
                await p.WaitForExitAsync();
            }
Esempio n. 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();
                }
            }
        }
Esempio n. 5
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);
        }
Esempio n. 6
0
        public virtual void EvalExec(RCRunner runner, RCClosure closure, RCString command)
        {
            RCAsyncState state   = new RCAsyncState(runner, closure, command);
            long         handle  = CreateHandle();
            ChildProcess process = new ChildProcess(this, handle, state, true);

            RegisterProcess(process);
            process.Start();
        }
Esempio n. 7
0
        public virtual void EvalSpawn(RCRunner runner, RCClosure closure, RCString command)
        {
            RCAsyncState state   = new RCAsyncState(runner, closure, command);
            long         handle  = CreateHandle();
            ChildProcess process = new ChildProcess(this, handle, state, false);

            RegisterProcess(process);
            process.Start();
            runner.Yield(closure, new RCLong(handle));
        }
Esempio n. 8
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();
            }
Esempio n. 9
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();
            }
Esempio n. 10
0
        protected void DoExec(object obj)
        {
            RCAsyncState state = (RCAsyncState)obj;

            try
            {
                ChildProcess process = (ChildProcess)state.Other;
                process.Start();
            }
            catch (Exception ex)
            {
                state.Runner.Report(state.Closure, ex);
            }
        }
        public void CanExecuteInAChildProcessAndRedirectOutputToString()
        {
            string command_line = "sample_test -t complex_numbers/add_complex_numbers";
            string result = string.Empty;
            var proc = new ChildProcess(command_line);
            proc.Start()
                .ContinueWith(
                (task) =>
                {
                    result = task.Result;
                }).Wait();

            const string expected_result = "Running 1 test case...\r\n\r\n*** No errors detected\r\n";
            Assert.AreEqual(expected_result, result);
        }
Esempio n. 12
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"));
        }
Esempio n. 13
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);
        }
        public void Run(string testExecutable, string testName, ITestOutput output)
        {
            var commandLineGenerator = new CommandLineGenerator();
            var options = TestRunnerOptions.FromDteProperties(_dte.Properties["BoostTest", "TestRunner"]);
            var commandLine = commandLineGenerator.Generate(testExecutable, testName, options);

            var testRunnerProcess = new ChildProcess(commandLine);
            testRunnerProcess.Start()
                    .ContinueWith(
                    result =>
                    {
                        if (result.IsCompleted)
                        {
                            output.Clear();
                            output.OutputString(DateTime.Now.ToLongTimeString() + " : Test : " + testName + "\n");
                            output.OutputString(result.Result);
                        }
                    });
        }
Esempio n. 15
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);
        }
Esempio n. 16
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();
            }
        }
Esempio n. 17
0
        public async Task Run(string installerPath)
        {
            var libraryPath = Path.Combine(BmclCore.MinecraftDirectory, "libraries");
            var archive     = new ZipArchive(new FileStream(installerPath, FileMode.Open));

            var entry = archive.GetEntry("version.json");

            if (entry == null)
            {
                throw new Exception("cannot find version.json");
            }

            var versionJson = new JSON <VersionInfo>().Parse(entry.Open());

            foreach (var jsonLibrary in versionJson.Libraries)
            {
                if (jsonLibrary.Name.StartsWith("net.minecraftforge:forge:"))
                {
                    continue;
                }
                if (jsonLibrary.IsVaildLibrary(libraryPath))
                {
                    continue;
                }
                ProgressChange("{DownloadingLibrary} " + jsonLibrary.Name);
                await BmclCore.MirrorManager.CurrentMirror.Library.DownloadLibrary(jsonLibrary,
                                                                                   Path.Combine(libraryPath, jsonLibrary.GetLibraryPath()));
            }

            entry = archive.GetEntry("install_profile.json");
            if (entry == null)
            {
                throw new Exception("cannot find install_profile.json");
            }
            var profileJson = new JSON <InstallerProfileScheme>().Parse(entry.Open());

            foreach (var profileJsonLibrary in profileJson.Libraries)
            {
                if (profileJsonLibrary.IsVaildLibrary(libraryPath))
                {
                    continue;
                }
                ProgressChange("{DownloadingLibrary} " + profileJsonLibrary.Name);
                await BmclCore.MirrorManager.CurrentMirror.Library.DownloadLibrary(profileJsonLibrary,
                                                                                   Path.Combine(libraryPath, profileJsonLibrary.GetLibraryPath()));
            }

            var buffer = Resources.forge_installer;
            // var stream = Application.GetResourceStream(new Uri("pack://application:,,,/forge_installer"));
            var installerHelperPath = Path.Combine(BmclCore.TempDirectory, "forge-installer-helper.jar");
            var fs = new FileStream(installerHelperPath, FileMode.Create);
            await fs.WriteAsync(buffer, 0, buffer.Length);

            fs.Close();

            var arguments = new List <string>();

            arguments.AddRange(new[]
                               { "-cp", $"{installerHelperPath};{installerPath}", "com.bangbang93.ForgeInstaller", _path });

            var cp = new ChildProcess(BmclCore.Config.Javaw, arguments.ToArray());

            cp.Start();

            cp.OnStdOut += (sender, log) =>
            {
                ProgressChange(log);
                Logger.Info(log);
            };
            cp.OnStdErr += (sender, log) => Logger.Fatal(log);

            await cp.WaitForExitAsync();
        }