I'm writing trying to write a SSH client. Im using the windows forms NOT the console app. I dont want to use it becasue I want to learn how tog et this to work... Anyways onwards to the question. I have a while loop which is running as long as my shell is open. BUT in order to send input via my textbox to the Ssh server I need it to wait for input. I have added an event listener that listens for ENTER KEY. And to fetch then input i have a function which returns the data. Inside that function is a while loop, which is run as long as a variable is true. The whole thing about listing for enter was that i would change the varibale that keept the while inside my function running so that it exited that and returned the data inside the textbox.
So I need a way to overide the while loop inside the function and to set the variable to false. I hae heard about override aswell as the threading things but Im not sure on what to do.
Here's my code!
//Variables
public string mHost;
SshShell mShell;
public string mInput;
string pattern = "";
bool mInputHolder = true;
//Initiate form!
public Form1()
{
InitializeComponent();
txthost.Text = "sdf.org";
txtuser.Text = "kalle82";
txtpass.Text = "XXXX";
string pattern = "sdf:";
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:\\\\.*>, etc...]: ");
//Stop for user input
mShell.ExpectPattern = pattern;
mShell.RemoveTerminalEmulationCharacters = true;
while (mShell.ShellOpened)
{
txtOutput.AppendText("\r\n" + "TERMINAL MODE ENGAGED");
txtOutput.AppendText(mShell.Expect( pattern ));
string data = userInput();
if (data == "") break;
//Data from termninal --> Append to text
string output = mShell.Expect(Pattern);
txtOutput.AppendText(output);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void checkforenter(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
mInputHolder = false;
}
}
public string userInput()
{
while (mInputHolder == true)
{
}
mInputHolder = true;
return txtInput.Text;
}