This bug tricked me into spending 4 hours debugging perfectly functional code last night.
Basically in certain situations c# isn’t updating a text label’s text, instead it blanks it.
I’ll give you the setup. The code I’m writing currently updates a text label with strings streamed over a network connection. Since it’s coming in from a networking connection, it’s also coming in from a background thread so I have to use Control.Invoke.
If:
I send one or two strings it updates just fine.
I tell it to put the string data on a button control’s text property it does it just fine. No matter how fast it comes in.
However it doesn’t display on labels if it comes in too fast. The label text is blanked instead, but occasionally I’ll see brief flashes of data. I’ve tried reading the label text property from other controls and it’s actually blanked of text, not just displaying nothing. Stopping the data stream leaves it blank.
Here’s the actual code if that helps.
//oncreate event
public debugScreen() {
InitializeComponent();
//used incase guiUdater is ran from the wrong thread
guiupdate = new guiUpdateDelegate(guiUpdaterFromBackgroundThread);
}
//delegate and creation defininiton for above
private delegate Delegate guiUpdateDelegate(Int32 location, string labelText);
guiUpdateDelegate guiUpdate;
//updates the form control
public void guiUpdater(string labelName, string labelText) {
//gets theindex psoition of the control in the form's Control array, returns -1 if not found
Int32 location = findControlIndexNumberFromName(labelName);
//if the item is found
if (location >= 0) {
//if this function was invoked from the wrong thread
if (this.Controls[location].InvokeRequired) {
//invoke the delagate with the function to handle gui updating from the corrext thread.
//location is the index number of the array in the form's Control array
//labelText is the new value for the label
this.Controls[location].Invoke(guiupdate, location, labelText);
}
else {
//if in correct thread just update the bloody thing
this.Controls[location].Text = labelText;
}
}
else {
//if control was not found (location = -1) redo with error on label 9
guiUpdater("Label9","not found: " + labelName);
}
}
//updates the text label
private Delegate guiUpdaterFromBackgroundThread(Int32 location, string labelText) {
Controls[location].Text = labelText;
return null;
}
It doesn’t do anything essential, just debug output, and since it updates just fine on buttons I know the data is getting through. So if this is the limit of this problem I can live with it. However I’m pretty curious what’s going.