BrandonPotter / SimpleTCP

Straightforward .NET library to handle the repetitive tasks of spinning up and working with TCP sockets (client and server).
Apache License 2.0
366 stars 108 forks source link

Why I can't update textbox.text with the received message? #13

Closed fralurbe closed 7 years ago

fralurbe commented 7 years ago

My textbox text doesn't change.

private void Server_DataReceived(object sender, SimpleTCP.Message message) { textBoxReceived.Text = message.MessageString; }

BrandonPotter commented 7 years ago

Probably because that event is coming from another thread, and all UI updates must happen on the main thread. See this q for how to update a forms control from another thread.

http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c

fralurbe commented 7 years ago

Thank you. Working now using this code, extracted from your link to stack overflow. private void Server_DataReceived(object sender, SimpleTCP.Message message) { ModifyText(message.MessageString); } private void ModifyText(string message) { if (textBoxRecibido.InvokeRequired) { Action a = () => ModifyText(message) ); Invoke(a); } else textBoxReceived.Text = message) ; }

BrandonPotter commented 7 years ago

Awesome!