void CheckLoop() { while (CheckThread != null) { IsInjected = false; CurrentProcess = Process.GetProcessesByName(ProcessName).FirstOrDefault(); if (CurrentProcess != null) { if (ProcessFound != null) { ProcessFound(this, CurrentProcess); } CurrentProcess.WaitForExit(); if (ProcessExited != null) { ProcessExited(this, CurrentProcess); } } else { Task.Delay(100).Wait(); } } }
public virtual void WaitForExit() { if (CurrentProcess != null) { CurrentProcess.WaitForExit(); } }
public static void RunGraph(string graph_path, ReadInput input_function) { if (CurrentProcess != null && !CurrentProcess.HasExited) { CurrentProcess.Kill(); } CurrentProcess = FetchNewProcess(graph_path); Aborting = false; if (input_function != null) { CurrentProcess.StartInfo.RedirectStandardInput = true; } try { //Start process and asynchronous reading CurrentProcess.Start(); CurrentProcess.BeginOutputReadLine(); CurrentProcess.BeginErrorReadLine(); if (input_function != null) { using (StreamWriter writer = new StreamWriter(CurrentProcess.StandardInput.BaseStream, Encoding.ASCII)) { while (!CurrentProcess.HasExited && !Aborting) { byte[] input = input_function(); if (input != null && input.Length != 0) { writer.WriteLine(Encoding.ASCII.GetString(input)); } else { writer.Flush(); } } } } if (Aborting && !CurrentProcess.HasExited) { CurrentProcess.Kill(); CurrentProcess.WaitForExit(); VSLogger.Log("Aborted current process."); } } catch (Win32Exception exception) { VSLogger.LogError("Output:\n" + exception.ToString()); } CurrentProcess.WaitForExit(); CurrentProcess = null; Aborting = false; }
public void KillOpenVpn() { if ((CurrentProcess != null)) { CurrentProcess.Kill(); CurrentProcess.WaitForExit(1000); CurrentProcess = null; } }
private async Task <CommandResult> ExecuteAsyncInternal(string executable, string args) { var stdOut = new List <String>(); var stdErr = new List <String>(); CurrentProcess = CreateProcess(executable, args); CurrentProcess.ErrorDataReceived += (s, e) => { stdErr.Add(e.Data); var handler = ErrorDataReceived; if (handler != null) { handler(s, e); } }; CurrentProcess.OutputDataReceived += (s, e) => { stdOut.Add(e.Data); var handler = OutputDataReceived; if (handler != null) { handler(s, e); } }; var completionTask = CurrentProcess.StartAndWaitForExitAsync(); CurrentProcess.BeginOutputReadLine(); CurrentProcess.BeginErrorReadLine(); await completionTask; if (!CurrentProcess.WaitForExit(TimeoutMiliseconds)) { throw new TimeoutException($"The process failed to exit after {TimeoutMiliseconds / 1000.0} seconds."); } RemoveNullTerminator(stdOut); RemoveNullTerminator(stdErr); return(new CommandResult( CurrentProcess.StartInfo, CurrentProcess.ExitCode, String.Join(System.Environment.NewLine, stdOut), String.Join(System.Environment.NewLine, stdErr))); }
private CommandResult RunProcess(string executable, string args, StreamForwarder stdOut, StreamForwarder stdErr) { CurrentProcess = StartProcess(executable, args); var taskOut = stdOut.BeginRead(CurrentProcess.StandardOutput); var taskErr = stdErr.BeginRead(CurrentProcess.StandardError); CurrentProcess.WaitForExit(); Task.WaitAll(taskOut, taskErr); var result = new CommandResult( CurrentProcess.StartInfo, CurrentProcess.ExitCode, stdOut.CapturedOutput, stdErr.CapturedOutput); return(result); }
private async Task <CommandResult> ExecuteAsyncInternal(string executable, string args) { var output = new List <string>(); CurrentProcess = CreateProcess(executable, args); CurrentProcess.ErrorDataReceived += (s, e) => { if (e.Data == null) { return; } output.Add($"[{_label}] {e.Data}"); Console.WriteLine($"[{_label}] {e.Data}"); ErrorDataReceived?.Invoke(s, e); }; CurrentProcess.OutputDataReceived += (s, e) => { if (e.Data == null) { return; } output.Add($"[{_label}] {e.Data}"); Console.WriteLine($"[{_label}] {e.Data}"); OutputDataReceived?.Invoke(s, e); }; var completionTask = CurrentProcess.StartAndWaitForExitAsync(); CurrentProcess.BeginOutputReadLine(); CurrentProcess.BeginErrorReadLine(); await completionTask; CurrentProcess.WaitForExit(); RemoveNullTerminator(output); return(new CommandResult( CurrentProcess.StartInfo, CurrentProcess.ExitCode, string.Join(System.Environment.NewLine, output))); }
private CommandResult RunProcess(string executable, string args, StreamForwarder stdOut, StreamForwarder stdErr) { Task taskOut = null; Task taskErr = null; CurrentProcess = CreateProcess(executable, args); CurrentProcess.Start(); try { taskOut = stdOut.BeginRead(CurrentProcess.StandardOutput); } catch (System.InvalidOperationException e) { if (!e.Message.Equals("The collection has been marked as complete with regards to additions.")) { throw; } } try { taskErr = stdErr.BeginRead(CurrentProcess.StandardError); } catch (System.InvalidOperationException e) { if (!e.Message.Equals("The collection has been marked as complete with regards to additions.")) { throw; } } CurrentProcess.WaitForExit(); var tasksToAwait = new List<Task>(); if (taskOut != null) { tasksToAwait.Add(taskOut); } if (taskErr != null) { tasksToAwait.Add(taskErr); } if (tasksToAwait.Any()) { try { Task.WaitAll(tasksToAwait.ToArray()); } catch (System.ObjectDisposedException e) { taskErr = null; taskOut = null; } } var result = new CommandResult( CurrentProcess.StartInfo, CurrentProcess.ExitCode, stdOut?.CapturedOutput ?? CurrentProcess.StandardOutput.ReadToEnd(), stdErr?.CapturedOutput ?? CurrentProcess.StandardError.ReadToEnd()); return result; }
private async Task <CommandResult> ExecuteAsyncInternal(string executable, string args) { var stdOut = new List <String>(); var stdErr = new List <String>(); CurrentProcess = CreateProcess(executable, args); CurrentProcess.ErrorDataReceived += (s, e) => { stdErr.Add(e.Data); var handler = ErrorDataReceived; if (handler != null) { handler(s, e); } }; CurrentProcess.OutputDataReceived += (s, e) => { stdOut.Add(e.Data); var handler = OutputDataReceived; if (handler != null) { handler(s, e); } }; var completionTask = StartAndWaitForExitAsync(CurrentProcess); CurrentProcess.BeginOutputReadLine(); CurrentProcess.BeginErrorReadLine(); await completionTask; CurrentProcess.WaitForExit(); RemoveNullTerminator(stdOut); RemoveNullTerminator(stdErr); var stdOutString = String.Join(System.Environment.NewLine, stdOut); var stdErrString = String.Join(System.Environment.NewLine, stdErr); if (!string.IsNullOrWhiteSpace(stdOutString)) { Logger.LogDebug("stdout: {out}", stdOutString); } if (!string.IsNullOrWhiteSpace(stdErrString)) { Logger.LogDebug("stderr: {err}", stdErrString); } return(new CommandResult( CurrentProcess.StartInfo, CurrentProcess.ExitCode, stdOutString, stdErrString)); }