Wednesday, January 6, 2010

Updateing TextBox from separete thread

I created a little web form app to send messages to a port. This was threaded and I wanted to update the UI with messages. So I had to use this:


Call :
UpdateSendTextBox(pmsData);

Code:
public void UpdateSendTextBox(string value)
{
if (InvokeRequired)
{
this.Invoke(new Action(UpdateSendTextBox), new object[] { value });
return; // important to return in a recursive call.
}
if(!sendingTextBox.IsDisposed) // will not update if application is closing
sendingTextBox.Text = value;
}

It will recursively call the UpdateSendTextBox with a new ui thread to update. That's it.

Life is good.

No comments: