I am using the MVC model with WPF, and am having difficulties with the UI locking up. The application is communicating between two endpoints, and the UI has a TextBox that I write detailed logs of the communication to. The code may either send a single file or multiple files in a burst.
I have a specific logging class that I use to process the logs, since they need to be created, formatted, and written to disk. I pass the TextBox to the Logging class, and then use a Dispatcher to write to it, but when I have several logs writing at the same time, the UI locks up. If I am only writing logs from a single sent file, everything works fine.
I have tried adding a Thread Lock, but this doesn't seem to fix anything. I've even locked the entire writing process (processing the information, writing to disk, and writing to the UI), but continue to get the same locking problems.
The code I use to write to the UI is listed below:
/// <summary>
/// Post the message to the UI.
/// </summary>
/// <param name="message">Message to post.</param>
/// <param name="scrollToEnd">Scroll to the end of the Text Box if true</param>
private void PostMessage(String message, bool scrollToEnd)
{
LoggingBox.Dispatcher.BeginInvoke(
System.Windows.Threading.DispatcherPriority.Normal,
new Action(delegate()
{
LoggingBox.AppendText(message);
if (scrollToEnd)
LoggingBox.ScrollToEnd();
}
));
}
The LogBox is just a TextBox:
/// <summary>
/// Location for logs to be displayed.
/// </summary>
public TextBox LoggingBox { get; set; }
This is my first time working with WPF,so I am sure there is a simple mistake that I am making. Any insight would be greatly appreciated.