Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I can't speak specifically to the SSH.NET and how you can cancel a command via that library, but here's how I would approach it.</p> <p>I would launch the "command" on a Task, which is essentially a separate thread. Here's my sample code:</p> <pre><code>private Task _commandTask; private CancellationTokenSource _cancellation; protected void SendCommand(object sender, EventArgs e) { if(_commandTask != null) return; // Can't run the task twice _cancellation = new CancellationTokenSource(); _commandTask = new Task(SendSshCommand, _cancellation.Token); _commandTask.ContinueWith(FinishCommand, TaskScheduler.FromCurrentSynchronizationContext()); _commandTask.Start(); // returning control to the UI thread now } protected void CancelCommand(object sender, EventArgs e) { if(_commandTask == null) return; _cancellation.Cancel(); } private static void SendSshCommand() { // Send your SSH Commands here } private void FinishCommand(Task task) { // Update your UI here // remove this task _commandTask = null; _cancellation = null; } </code></pre> <p>So the <code>SendCommand</code> method would be invoked from some event on your UI (button click I assume). It creates a <code>CancellationTokenSource</code> and a new <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task%28VS.100%29.aspx" rel="nofollow">Task</a>. This task calls the <code>SendSshCommand</code> method on a separate thread. We then create a task continuation with the <code>ContinueWith</code> method. Passing in the <code>TaskScheduler.FromCurrentSyncrhonizationContext()</code> tells the continuation task that we want to execute on the UI's thread instead of a separate thread. This is important because if we're not on the UI thread we cannot access any of the UI controls.</p> <p>The <code>CancelCommand</code> would be called from your cancel button, and it simply tells the <code>_commandTask</code> to stop executing.</p> <p>Now this isn't 100%, I'm sure you're going to have state that you need to pass around to the tasks, and there are <a href="http://msdn.microsoft.com/en-us/library/dd321424.aspx" rel="nofollow">Generic Task</a> structures to help you with that. Also, in your <code>FinishCommand</code>, you're going to want to check to make sure the task was completed, not cancelled, and didn't throw an exception. Read up on Tasks and the TPL in .Net 4 and you'll see how to do that.</p> <p>It's also worth noting, that I'm not sure what will happen with your SSH.NET library after the cancellation. I would assume it would just disconnect the SSH session, which in turn would end the process on the Unix machine. But again, I'm not familiar with that particular library so you'll have to do some testing.</p> <p>Hope that helps!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload