title: Using a SSH library to connect to unix and tail a file: Is this the right approach?
body: <p>As Ive stated with a few other questions, Ive been using a <a href="http://sshnet.codeplex.com/" rel="nofollow">new SSH .NET library</a> to connect to a Unix server and run various scripts and commands.  Well, I've finally attempted to use it to run a Unix tail -f on a live log file and display the tail in a Winforms RichTextBox.</p>
  
  <p>Since the library is not fully-fleshed out, the only kinda-sorta solution I've come up with seems lacking... like the feeling you get when you know there has to be a better way.  I have the connection/tailing code in a separate thread as to avoid UI thread lock-ups.  This thread supports cancellation request (which will allow the connection to gracefully exit, the only way to ensure the process Unix side is killed).  Here's my code thus far (which for the record seems to work, I would just like some thoughts on if this is the right way to go about it):</p>
  
  <pre><code>PasswordConnectionInfo connectionInfo = new PasswordConnectionInfo(lineIP, userName, password);
  
  string command = "cd /logs; tail -f " + BuildFileName() + " \r\n";
  
  using (var ssh = new SshClient(connectionInfo))
  {
      ssh.Connect();
  
      var output = new MemoryStream();
      var shell = ssh.CreateShell(Encoding.ASCII, command, output, output);
  
      shell.Start();
  
      long positionLastWrite = 0;
  
      while (!TestBackgroundWorker.CancellationPending) //checks for cancel request  
      {
          output.Position = positionLastWrite;
  
          var result = new StreamReader(output, Encoding.ASCII).ReadToEnd();
          positionLastWrite = output.Position;
          UpdateTextBox(result);
  
          Thread.Sleep(1000);
      }
  
      shell.Stop();
      e.Cancel = true;
  }
  </code></pre>
  
  <p>The UpdateTextBox() function is a thread-safe way of updating the RichTextBox used to display the tail from a different thread. The positionLastWrite stuff is an attempt to make sure I don’t loose any data in between the Thread.Sleep(1000).  </p>
  
  <p>Now Im not sure about 2 things, first being that I have the feeling I might be missing out on some data each time with the whole changing MemoryStream position thing (due to my lack of experiance with MemoryStreams, and the second being that the whole sleep for 1 second then update again thing seems pretty archaic and inefficient... any thoughts?</p>