Reputation: 163
I am trying to update the Rich Text Box from the event handler when the message arrive. For some reason the rich text box only updates in the end when all messages have arrived.
Code I am using:
private void OutputMessageToLogWindow(string message)
{
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
outputRichTxtBox.AppendText(message);
test.Text = message;
}));
}
Upvotes: 0
Views: 1014
Reputation: 1507
What I think is that your code is not thread-safe, in case of concurrent messages it is possible that some of the messages will not be updated by executing the following lines at the same time:
outputRichTxtBox.AppendText(message);
test.Text = message;
So to make it thread-safe, I would recommend to use lock
inside your BeingInvoke
method:
private static readonly object synchLock = new object();
private void OutputMessageToLogWindow(string message)
{
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
lock(synchLock)
{
outputRichTxtBox.AppendText(message);
test.Text = message;
}
}));
}
Upvotes: 1