c# - Emulating console in winforms, the hard way how to make it better -


im trying emulate console in windows forms applicaton. have made possible using 2 threads , delegate able interact multiline textbox.

this somehow seems complicate things much. questions.

  1. is there better way of doing this?
  2. when press enter command not sent, first if press again sent? why that? ahve treid debug failed find solution.

edit! im using csharpssh, ssh connection. have included 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 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 thread id of         // calling thread thread id of creating thread.         // if these threads different, 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     {                 {             return txthost.text;         }         set         {             txthost.text = value;         }      }      public string user     {                 {             return txtuser.text;         }         set         {             txtuser.text = value;         }      }      public string pass     {                 {             return txtpass.text;         }         set         {             txtpass.text = value;         }      }      public string pattern     {                 {             return pattern;         }          set         {             pattern = value;         }      }      private void button2_click(object sender, eventargs e)     {         clientopen = false;     }    }  } 


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -