I am receiving a struct from a server app and when the client program gets it and its the type of serverInfo it sends it to a functions and sets the server info to labels. I have put MessageBox.Show() on both ends to see whats wrong and the first one pops up but the second doesnt. The first messagebox shows the info from the struct recived from the server and its all correct so i know that works. I have also put a Message box between each of the label.text = text and they didnt show. I also tried just putting “this is a test string” on them and they still didnt do anything.
Edit* this is WinForms
Edit* I found out that it is a cross threading problem so whats a good way to change labels from another thread?
private void onServerInfo(msgBox message)
{
serverInfo.info info = (serverInfo.info)message.getMessage("info");
MessageBox.Show(info.name + " ; " + info.type + " ; " + info.limit); // works with everything showing up right
ServerName.Text = info.name; //this is a string
ServerType.Text = info.type.ToString(); // this is a enum
MaxLimit.Text = info.limit.ToString(); // this is a int
MessageBox.Show(ServerName.Text + " ; " + ServerType.Text + " ; " + MaxLimit.Text); // doesnt ever show
}
Since your exception is caused by a cross-threading access exception, use this handy method to fix that:
https://stackoverflow.com/a/2242393/555547
So edit your class to be like:
Though for good practice, you should really put that extension method in it’s own file called ControlExtensions.cs and call it from there.
Edit: Actually, you must put that extension method in it’s own static class or you’ll get an error.