I am developing a GUI where a user can connect to server and read the data. The data needs to be displayed on the GUI. For this I am using TabControl whose ContentTemplate is set to RichTextBox.
When the user click connect button, the UI connects to the TCP server. Here I spawn a new thread which reads data from the server. However after sometime when huge data is pumped to the RTB, the UI becomes unresponsive. I cannot move the app within the screen.
Is there any way where I can remove the older data dynamically (new data gets appeneded below) so that the RTB is not loaded much and UI should work properly. Also the app is utilizing upto 80 CPU under Processes in Task Manager
private void BtnConnect_Click(object sender, RoutedEventArgs e)
{
//connect
TCPClientClass tcpClient = TCPConnHandler.ConnectToService(tbIPAddress.Text);
if (tcpClient != null)
{
MessageBox.Show("Connected to " + tbIPAddress.Text);
//open new tab
var item = MainWindowVMObj.AddTabItem();
Thread thTabControl = new Thread(() =>
{
while (tcpClient.Connected)
{
String str = tcpClient.GetDataFromServer();
if (!String.IsNullOrEmpty(str))
tabControl1.Dispatcher.BeginInvoke((Action)(() => item.Content += str));
Thread.Sleep(200);
}
//item.Dispatcher.BeginInvoke
});
thTabControl.Start();
}
}