0

Im trying to emulate the console in a windows forms applicaton. I have made it possible by using two extra threads and a delegate to be able to interact with my multiline textbox.

This somehow seems like I complicate things to much. So my questions.

  1. Is there a better way of doing this?
  2. When i press enter the command does not get sent, first if i press again it get sent? WHy is that? I ahve treid to debug it but failed to find the solution.

EDIT! Im using CsharpSSH, to do the SSH connection. Also I have included my full code now!

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Tamir.SharpSsh;
    using System.IO;
    using System.Threading;
    using System.Timers;


    namespace WindowsFormsApplication3
    {


    public partial class Form1 : Form
    {

    public string mHost;
    SshShell mShell;
    public string mInput;
    string pattern = "";
    bool mInputHolder = false;
    string mPattern = "";
    int mValue = 0;
    bool mStatus = false;
    private Thread thrdtwo = null;
    private Thread thrdone = null;
    public string mKorv;
    string mString = "";
    delegate void SetTextCallback(string text);
    bool clientopen = true;



    public Form1()
    {
        InitializeComponent();

        txthost.Text = "sdf.org";
        txtuser.Text = "kalle82";
        txtpass.Text = "kattsand";
        string pattern = "sdf:";
        mPattern = pattern;

    }

    public void button1_Click(object sender, EventArgs e)
    {

        mShell = new SshShell(Host, User);
        mShell.Password = Pass;
        //WRITING USER MESSAGE
        txtOutput.AppendText("Connecting...");
        mShell.Connect();
        txtOutput.AppendText("OK");
        mShell.ExpectPattern = mPattern;
        mShell.RemoveTerminalEmulationCharacters = true;
        this.SetText(mShell.Expect(pattern)); 
        txtInput.Focus();

        thrdone = new Thread(new ThreadStart(appengine));
        thrdone.Start();      

    }


    private void appengine()
    {
        this.txtInput.KeyPress += new System.Windows.Forms.KeyPressEventHandler(checkforenter);

      //  MessageBox.Show("Appengine started");

        while (mShell.ShellOpened)
        {

            thrdtwo = new Thread(new ThreadStart(startthread2));
            thrdtwo.Start();
            thrdtwo.Join();     


           // this.SetText(mShell.Expect(pattern));
            if (clientopen == false) break;
        }

       // MessageBox.Show("Appengine stopped");

    }



    private void startthread2()
    {

        //Wait for answer
        while (mStatus == false)
        {

        }

    }

    //Recieves keypressevent
    public void checkforenter(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)13)
        {

            mStatus = true;
            mString = txtInput.Text;
            mShell.WriteLine(mString);
            this.SetText(mShell.Expect(pattern));
            txtOutput.AppendText(txtInput.Text + "\n");
        }

        mStatus = false;
    }

    private void SetText(string text)
    {
        // InvokeRequired required compares the thread ID of the
        // calling thread to the thread ID of the creating thread.
        // If these threads are different, it returns true.
        if (this.txtOutput.InvokeRequired)
        {
            SetTextCallback d = new SetTextCallback(SetText);
            this.Invoke(d, new object[] { text });
        }
        else
        {
            this.txtOutput.Text = text.ToString();
        }
    }




    public int checkfortrue()
    {

        if (mInputHolder != true)
        {
            mValue = 0;
        }
        if (mInputHolder == false)
        {
            mValue = -1;
        }
        return mValue;
    }


    public string userInput()
    {
        while (mInputHolder == true)
        {

        }
        mInputHolder = true;
        return txtInput.Text;
    }
    //Properties
    public string Host
    {
        get
        {
            return txthost.Text;
        }
        set
        {
            txthost.Text = value;
        }

    }

    public string User
    {
        get
        {
            return txtuser.Text;
        }
        set
        {
            txtuser.Text = value;
        }

    }

    public string Pass
    {
        get
        {
            return txtpass.Text;
        }
        set
        {
            txtpass.Text = value;
        }

    }

    public string Pattern
    {
        get
        {
            return pattern;
        }

        set
        {
            pattern = value;
        }

    }

    private void button2_Click(object sender, EventArgs e)
    {
        clientopen = false;
    }



}

}
8bitcat
  • 2,206
  • 5
  • 30
  • 59
  • Can you make your question better, I spent a good 5 minutes trying your code. a) put the basic InitaliseComponent code in the form so we dont have to design your form. b) mention the references to make it compile eg Rebex.net. c) please tell us http://WhatYouHaveTried.com? What example are you following? – Jeremy Thompson Jun 04 '12 at 21:20
  • Have you seen this? http://www.codeproject.com/Articles/9621/ShellControl-A-console-emulation-control – Magnus Johansson Jun 04 '12 at 21:22
  • @JeremyThompson I have InitializeComponent(); in my code above?! if that what you mean.. Also This code is working but when looking it at it working with 3 different threads cant be the best way of solving this problem? I have tried with two threads but never got that to work. So im looking for an alternative way of solving my problem. Im using the csharpssh dll for my application. – 8bitcat Jun 04 '12 at 22:06
  • @MagnusJohansson yes i ahve seen that, but i want to build it on my own. So im looking for a better way to program this. – 8bitcat Jun 04 '12 at 22:06
  • could you just use an actual console window, via `AllocConsole` and `Console.Read`/`Console.Write`? – Michael Edenfield Jun 04 '12 at 22:11
  • @CarlPalsson see how much easier it is to just copy the code here: http://stackoverflow.com/questions/10526118/bug-cant-choose-dates-on-a-datepicker-that-fall-outside-a-floating-vsto-add-in/10564216#10564216 All the designer code has been specified and not hidden in the InitComponent(), thats all I was saying. – Jeremy Thompson Jun 04 '12 at 22:13

0 Answers0