Asynchronous.aml 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <topic id="3e7487ed-9a0c-4442-8b31-14e291a7c648" revisionNumber="1">
  3. <developerSDKTechnologyOverviewOrientationDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
  4. <title>Execut command asynchronously</title>
  5. <content>
  6. If you need to execute long running command you might want to execute it in asynchronously.
  7. To execute command asynchronously you need to create command first and then call <codeEntityReference>M:Renci.SshNet.SshCommand.BeginExecute(System.AsyncCallback, System.Object)</codeEntityReference> method
  8. <alert class="note">
  9. <para>If an exception occures while command is being executed it will be thrown when <codeEntityReference>M:Renci.SshNet.SshCommand.EndExecute(System.IAsyncResult)</codeEntityReference> is called.</para>
  10. </alert>
  11. <para>See examples below:</para>
  12. <codeExample>
  13. <list class="ordered">
  14. <listItem>
  15. <para>Display exit status when command is executed.</para>
  16. <code language="cs">
  17. using (var client = new SshClient("host", "username", "password"))
  18. {
  19. client.Connect();
  20. var cmd = client.CreateCommand("sleep 30s;date"); // Perform long running task
  21. var asynch = cmd.BeginExecute(null, null);
  22. while (!asynch.IsCompleted)
  23. {
  24. Console.WriteLine("Waiting for command to complete...");
  25. Thread.Sleep(2000);
  26. }
  27. cmd.EndExecute(asynch);
  28. client.Disconnect();
  29. }
  30. </code>
  31. </listItem>
  32. </list>
  33. </codeExample>
  34. </content>
  35. <relatedTopics>
  36. <codeEntityReference>M:Renci.SshNet.SshCommand.BeginExecute(System.AsyncCallback, System.Object)</codeEntityReference>
  37. <codeEntityReference>M:Renci.SshNet.SshCommand.EndExecute(System.IAsyncResult)</codeEntityReference>
  38. <codeEntityReference>T:Renci.SshNet.SshCommand</codeEntityReference>
  39. </relatedTopics>
  40. </developerSDKTechnologyOverviewOrientationDocument>
  41. </topic>