0

我试图在 Windows 窗体应用程序中模拟控制台。我通过使用两个额外的线程和一个委托来与我的多行文本框进行交互,使之成为可能。

这似乎让我把事情复杂化了很多。所以我的问题。

  1. 有没有更好的方法来做到这一点?
  2. 当我按下回车时,命令不会被发送,首先如果我再次按下它会被发送?这是为什么?我很想调试它,但找不到解决方案。

编辑!我使用 CsharpSSH 进行 SSH 连接。我现在也包含了我的完整代码!

    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;
    }



}

}
4

0 回答 0