Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7903871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:51:25+00:00 2026-06-03T09:51:25+00:00

I’m writing trying to write a SSH client. Im using the windows forms NOT

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:51:26+00:00Added an answer on June 3, 2026 at 9:51 am

    Essentially your problem is unsolvable. If you block your code in a button click to wait for user input you will be freezing the UI, which will prevent anyone from being able to provide said input; this is called a deadlock.

    You can open up the shell in the button click and then append to it later on after you have input, or you can wait until you already have the input to send to the shell before opening it in the first place.

    Since you asked for some code, here’s my ‘hello world of a shell’ example. I was actually using it to play around with WPF. There’s a textbox for input, a textbox to display output, and a button to send the data in the input textbox to the shell. Realize that this is neither a production quality example, nor is it specifically written in response to your question, but it should demonstrate the overall approach to the problem.

        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            private Process cmd;
            private bool scrollAtBottom = false;
            public MainWindow()
            {
                InitializeComponent();
    
                Closed+=new EventHandler(MainWindow_Closed);
                textBlock1.Text = "";
    
                textBox1.Focus();
            }
    
            private void button1_Click_1(object sender, RoutedEventArgs e)
            {
                if (cmd == null || cmd.HasExited)
                {
                    cmd = new Process();
                    cmd.StartInfo.CreateNoWindow = false;
                    cmd.StartInfo.FileName = "cmd.exe";
                    cmd.StartInfo.RedirectStandardInput = true;
                    cmd.StartInfo.RedirectStandardOutput = true;
                    cmd.StartInfo.UseShellExecute = false;
    
                    cmd.OutputDataReceived += new DataReceivedEventHandler(cmd_OutputDataReceived);
    
                    cmd.Start();
                    cmd.BeginOutputReadLine();
                }
    
                cmd.StandardInput.WriteLine(textBox1.Text);
    
                textBox1.Text = "";
            }
    
            private void cmd_OutputDataReceived(object sender, DataReceivedEventArgs e)
            {
                textBlock1.Dispatcher.Invoke(new Action(() =>
                {
                    textBlock1.Text += e.Data + Environment.NewLine;
                    scrollViewer1.ScrollToEnd();
                }));
            }
    
            private void MainWindow_Closed(object sender, EventArgs e)
            {
                if (cmd != null && !cmd.HasExited)
                {
                    //exit nicely
                    cmd.StandardInput.WriteLine("exit");
                    if (!cmd.HasExited)
                    {
                        //exit not nicely
                        cmd.Kill();
                    }
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
We're building an app, our first using Rails 3, and we're having to build
I am writing an app with both english and french support. The app requests
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am using Paperclip to handle profile photo uploads in my app. They upload
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.