I'm pretty new at C# so forgive me if this is a bit of a silly question. Anyway I'm writing a little chat application that so far seems to be going well, I've run into a problem that I seem to have fixed but I'm hoping to work out why I need to do it this way.
When I receive a message I want to update a text box so I use:
txtConnectedID.Text = "test";
But I receive this error:
System.InvalidOperationException was unhandled by user code
Message=Cross-thread operation not valid: Control 'txtConnectedID' accessed from a thread other than the thread it was created on.
Now, I think this has something to do with stopping the method running twice and not updating properly? I'm not 100% on this. So now I have a delegate for a method that accepts a string and I call:
private delegate void stringDelegate(string s);
BeginInvoke(new stringDelegate(writeToIPBox), new object[] { e.ConnectedIP.ToString() });
private void writeToIPBox(string newIP)
{
txtConnectedID.Text = newIP;
}
I'm not sure why I'm doing this, how it's any different. I'm not really happy to just do it this way without knowing why.
Thanks in advance