I've got some backgroundworker threads that are working on calculations or saving data. Every time a thread is done working I'm first adding a message to a static messagelist and then I'm showing this in a richtextbox.
The message contains the hour, a label, the message and a message type. This message type I'm using to show the message in a specific color.
Now I've got the problem that some times a 2 threads are done at the same time and try to set a message. So I got the cross-thread exception.
I know that I can solve this by using a delegate. But I'm kinda stuck here.
this is how I set my message currently:
private void SetMessages()
{
rtxtMessage.Text = "";
foreach (var message in GlobalVariables.MessageList)
{
var text = message.Date.ToShortTimeString() + " " + message.Label + ": " +
message.TheMessage;
switch (message.Type)
{
case GlobalVariables.MessageType.normal:
rtxtMessage.SelectionColor = Color.Black;
break;
case GlobalVariables.MessageType.calculation:
rtxtMessage.SelectionColor = Color.Green;
break;
case GlobalVariables.MessageType.error:
rtxtMessage.SelectionColor = Color.Red;
break;
case GlobalVariables.MessageType.warning:
rtxtMessage.SelectionColor = Color.Orange;
break;
default:
break;
}
rtxtMessage.SelectedText = text + Environment.NewLine;
rtxtMessage.ScrollToCaret();
}
pnlMessage.Visible = true;
}
So main question is how can I rewrite this to get it working with a delegate?