简体   繁体   中英

writing to/updating a TextBox in “real time” on a networked form C#

I have a windows form in visual studio 10 that connects to a python server program and receives a string of data repeatedly from the server. it then writes this string to a textbox. currently for testing purposes the server sends this string 10 times and then turns off. what i want the client to do is after it receives each string it writes it to the text box immediately but currently it waits until the server has finished and then writes the last string. the client code for this is as follows:

while (TestConnection(client))
                {
                   char tt = ' ';
                   try{
                       tt = Convert.ToChar(clientStreamReader.Read());
                       if (count < 17)
                       {
                           message = tt.ToString();
                           m += message;
                           count += 1;
                       }
                       else if (count >= 17)
                       {
                           textBox2.Text = m + "\r\n";
                           m = "";
                           count = 0;
                       }
                   }catch(OverflowException){
                            m += "";
                   }                
                }

how can I change this to write each string as they are received?

(TestConnection is a custom bool that indicates weather the connection is good) EDIT: (loop is inside button click event)

You could write an background Worker process, which always checks in a loop if there is a new string wich can be written to your textbox. Check out this link: example on CodeProject

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM