Everytime I need to update UI elements from a background thread I use the following code (here to update a textbox named "txtLog":
delegate void SetTextCallback(string text);
public void addLog(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.txtLog.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(addLog);
this.Invoke(d, new object[] { text });
}
else
{
txtLog.Text += text + "\r\n";
//scroll to updated text
txtLog.SelectionLength = 0;
txtLog.SelectionStart = txtLog.Text.Length - 1;
txtLog.ScrollToCaret();
}
}