//public event EventHandler Update async void ExecRoutine(CancellationToken Token) { Process proc = new Process(); proc.StartInfo.FileName = "CMD.exe"; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardInput = true; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.UseShellExecute = true; proc.StartInfo.CreateNoWindow = true; proc.StartInfo.UseShellExecute = false; proc.Start(); Task cmd = new Task((Action)(() => { for (int i = 0; i < cmds.Length; i++) { proc.StandardInput.WriteLine(cmds[i]); } })); cmd.Start(); while (!Token.IsCancellationRequested) { if (proc.HasExited) { break; } if (ProcessOutputHandler != null) { ProcessOutputHandler.Invoke(this, new ProcessEventArgs(proc.StandardOutput.ReadLine(), null)); } proc.Refresh(); await Task.Delay(1); } if (ProcessOutputHandler != null) { ProcessOutputHandler.Invoke(this, new ProcessEventArgs(null, proc.StandardError.ReadToEnd())); } if (!proc.HasExited) { proc.Kill(); } if (OnComplete != null) { OnComplete.Invoke(); } }
/// <summary> /// Discover npm's global prefix (the parent of its global module cache location). /// </summary> /// <returns> /// The global prefix reported by npm. /// </returns> /// <remarks> /// <para> /// To run Azure Functions locally in tests, we need to run the <c>func</c> command /// from the <c>azure-functions-core-tools</c> npm package. /// </para> /// <para> /// Unfortunately, npm ends up putting this in different places on different machines. /// Debugging locally, and also on private build agents, the global module cache is /// typically in <c>%APPDATA%\npm\npm_modules</c>. However, on hosted build agents it /// currently resides in <c>c:\npm\prefix</c>. /// </para> /// <para> /// The most dependable way to find where npm puts these things is to ask npm, by /// running the command <c>npm prefix -g</c>, which is what this function does. /// </para> /// </remarks> private static async Task <string> GetNpmPrefix() { // Running npm directly can run into weird PATH issues, so it's more reliable to run // cmd.exe, and then ask it to run our command - that way we get the same PATH // behaviour we'd get running the command manually. var processHandler = new ProcessOutputHandler( new ProcessStartInfo("cmd.exe", "/c npm prefix -g") { UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, }); processHandler.Start(); await Task.WhenAny( processHandler.ExitCode, Task.Delay(TimeSpan.FromSeconds(10))).ConfigureAwait(false); if (!processHandler.ExitCode.IsCompleted) { throw new FunctionStartupException( "npm task did not exit before timeout.", stdout: processHandler.StandardOutputText, stderr: processHandler.StandardErrorText); } processHandler.EnsureComplete(); if (processHandler.Process.ExitCode != 0) { throw new FunctionStartupException("Unable to run npm.", stderr: processHandler.StandardErrorText); } // We get a newline character on the end of the standard output, so we need to // trim before returning. return(processHandler.StandardOutputText.Trim()); }
private void runProcess(string FileName, string Arguments) { // prep process ProcessStartInfo psi = new ProcessStartInfo(FileName, Arguments); psi.UseShellExecute = false; psi.CreateNoWindow = true; psi.RedirectStandardOutput = true; psi.RedirectStandardError = true; // start process using (Process process = new Process()) { // pass process data process.StartInfo = psi; // prep for multithreaded logging ProcessOutputHandler outputHandler = new ProcessOutputHandler(process); Thread stdOutReader = new Thread(new ThreadStart(outputHandler.ReadStdOut)); Thread stdErrReader = new Thread(new ThreadStart(outputHandler.ReadStdErr)); // start process and stream readers process.Start(); stdOutReader.Start(); stdErrReader.Start(); // wait for process to complete //while (process.a //ActiveForm.Invalidate(); process.Exited += new EventHandler(processFileEvent); process.WaitForExit(); } }