title: C# - SSH Winforms Emulate console
body: <p>I know this questions ahs been asked a lot but I can't seem to fidn a good way to start.</p>
  
  <p>I have been using sharpssh, but it's made for a console application. Therefore when i try to create a winforms application I cant make it run the way I want.</p>
  
  <p>In my form I have: 
  A textbox which shows the ouput
  A textbox where the user should writes the command.</p>
  
  <p>Im stuck at the readline(), I need to PAUSE the application until the user hit's enter and then send the command inserted in the textbox. I dont know how to convert the application into a winform application. </p>
  
  <p><strong>So the question is how to go about this?</strong> 
  - Should I use processes one that starts when initializing the application, and one that starts when output is recieved and input is required? And use the second process to collect the input listen for the event enter key pressed in the textbox and the start the other process to send the input and wait for output again?</p>
  
  <p>If so any have an example on how to solve it?</p>
  
  <p>Here's the code?</p>
  
  <pre><code>    public Form1()
      {
          InitializeComponent();
  
          txthost.Text = "XXX";
          txtuser.Text = "XXXXX";
          txtpass.Text = "XXXXX";
          string pattern = "sdf:";
          mPattern = pattern;
          this.txtInput.KeyPress += new System.Windows.Forms.KeyPressEventHandler(checkforenter);
      }
  
      public void button1_Click(object sender, EventArgs e)
      {
  
          try
          {
  
              mShell = new SshShell(Host, User);
              mShell.Password = Pass;
              //WRITING USER MESSAGE
              txtOutput.AppendText("Connecting...");
              mShell.Connect();
              txtOutput.AppendText("OK");
              //txtOutput.AppendText("Enter a pattern to expect in response [e.g. '#', '$', C:\\\\.*&gt;, etc...]: ");
              //Stop for user input
  
              mShell.ExpectPattern = mPattern;
              mShell.RemoveTerminalEmulationCharacters = true;
              _writer = new TextBoxStreamWriter(txtOutput);
  
              Console.SetOut(_writer);
  
              StringReader reader = new StringReader(txtInput.Text);
  
              while (mShell.ShellOpened)
              {
                  txtOutput.AppendText("\r\n" + "TERMINAL MODE ENGAGED");
                  txtOutput.AppendText(mShell.Expect( pattern ));
  
                  txtInput.Text = Console.ReadLine();
                  Console.ReadLine();
                  Console.WriteLine(Environment.NewLine);
                  txtOutput.AppendText(mShell.Expect(pattern));
                  MessageBox.Show("innan enter 2");
                  Console.WriteLine(Environment.NewLine);
                  txtOutput.AppendText(mShell.Expect(("(continue)")));
                  MessageBox.Show("efter enter 2");
                  Console.WriteLine(Environment.NewLine);
                  //Data from termninal --&gt; Append to text
                  string output = mShell.Expect(Pattern);
                  txtOutput.AppendText(output);
              }
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
  
      }
  </code></pre>