/// <summary> Runs when background worker completes </summary> /// <param name="sender"></param> /// <param name="e"></param> private void worker_EncodeCompleted(object sender, RunWorkerCompletedEventArgs e) { if (cmdCurrent != null) { cmdCurrent.CancelAsync(); cmdCurrent = null; } if (procWnds != null) { if (!procWnds.HasExited) { procWnds.Kill(); } procWnds = null; } Globals.currentFileBeingEncoded = String.Empty; lstFilesToEncode.Clear(); bCurrentlyEncoding = false; btnStartStopEncode.Content = "START"; btnBack.IsEnabled = true; comboPresets.IsEnabled = true; if (workerEncode.CancellationPending) { txtOutput.AppendText("ENCODING CANCELLED BY USER\n"); } txtOutput.ScrollToEnd(); prgEncode.Value = 0; }
public void CancelAsyncTest() { Session session = null; // TODO: Initialize to an appropriate value string commandText = string.Empty; // TODO: Initialize to an appropriate value SshCommand target = new SshCommand(session, commandText); // TODO: Initialize to an appropriate value target.CancelAsync(); Assert.Inconclusive("A method that does not return a value cannot be verified."); }
public void CancelAsyncTest() { Session session = null; // TODO: Initialize to an appropriate value string commandText = string.Empty; // TODO: Initialize to an appropriate value SshCommand target = new SshCommand(session, commandText); // TODO: Initialize to an appropriate value target.CancelAsync(); Assert.Inconclusive("A method that does not return a value cannot be verified."); }
private static async Task CheckOutputAndReportProgress(SshCommand sshCommand, TextReader stdoutStreamReader, TextReader stderrStreamReader, IProgress <ScriptOutputLine> progress, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { sshCommand.CancelAsync(); } cancellationToken.ThrowIfCancellationRequested(); await CheckStdoutAndReportProgressAsync(stdoutStreamReader, progress); await CheckStderrAndReportProgressAsync(stderrStreamReader, progress); }
// ---------------- Properties ---------------- // ---------------- Functions ---------------- /// <summary> /// Runs the SSH process. /// </summary> /// <returns>The exit code of the process.</returns> public int RunSsh(CancellationToken cancelToken) { this.config.Validate(); this.lockFileManager.CreateLockFile(); using (SshClient client = new SshClient(this.config.Server, this.config.Port, this.config.UserName, this.config.Password)) { client.Connect(); this.logger.WarningWriteLine(2, "Client Version: " + client.ConnectionInfo.ClientVersion); this.logger.WarningWriteLine(2, "Server Version: " + client.ConnectionInfo.ServerVersion); using (SshCommand command = client.CreateCommand(this.config.Command)) { IAsyncResult task = command.BeginExecute(); // Using tasks seems to print things to the console better; it doesn't all just bunch up at the end. Task stdOutTask = AsyncWriteToStream(Console.OpenStandardOutput, command.OutputStream, task, "STDOUT", cancelToken); Task stdErrTask = AsyncWriteToStream(Console.OpenStandardError, command.ExtendedOutputStream, task, "STDERR", cancelToken); try { Task[] tasks = new Task[] { stdOutTask, stdErrTask }; Task.WaitAll(tasks, cancelToken); } catch (OperationCanceledException) { this.logger.WarningWriteLine(1, "Cancelling Task..."); command.CancelAsync(); this.logger.WarningWriteLine(1, "Task Cancelled"); this.lockFileManager.DeleteLockFile(); throw; } command.EndExecute(task); int exitStatus = command.ExitStatus; this.logger.WarningWriteLine(1, "Process exited with exit code: " + exitStatus); this.lockFileManager.DeleteLockFile(); return(exitStatus); } } }
/// <summary> /// Close the robot output stream /// </summary> /// <returns>True if successful</returns> public static Task <bool> CloseRobotOutputStream() { return(Task.Run(() => { if (outputStreamCommand != null && !openingOutputStream && !closingOutputStream) { try { closingOutputStream = true; outputStreamCommand.CancelAsync(); outputStreamCommand.Dispose(); outputStreamCommand = null; closingOutputStream = false; } catch (Exception e) { Debug.Log(e.ToString()); closingOutputStream = false; return false; } } return true; })); }
private async Task <SshCommand> RunCommandAsync(string command, ILogger logger, System.Func <SshCommand, IAsyncResult, Task> doWithResult = null, CancellationToken?cancellationToken = null) { CancellationToken token = cancellationToken ?? new CancellationToken(); SshCommand cmd = null; Exception lastException = null; for (int i = 0; i < Retries; i++) { try { cmd = this.client.CreateCommand(command); var tcs = new TaskCompletionSource <bool>(); var result = cmd.BeginExecute((asr) => { if (asr.IsCompleted) { tcs.SetResult(true); } }); using (var registration = token.Register(() => { Task.Run(() => cmd.CancelAsync()).ContinueWith(_ => tcs.TrySetResult(false)); })) { var tasks = new List <Task>(); if (doWithResult != null) { tasks.Add(doWithResult(cmd, result)); } tasks.Add(tcs.Task); await Task.WhenAll(tasks); }; cmd.EndExecute(result); lastException = null; break; } catch (SocketException e) { if (logger != null) { logger.Log("SocketException:\n" + e.Format()); } lastException = e; } catch (SshException e) { if (logger != null) { logger.Log("SshException:\n" + e.Format()); } lastException = e; } this.IsConnected = false; if (logger != null) { logger.Log("Trying to re-establish connection"); } await this.ConnectAsync(logger); } if (lastException != null) { throw lastException; } return(cmd); }