频繁写richTextBox1,暴内存错误(c#代码)
一个程序中有两个线程,一个线程往listSearchLines中每1秒加入文字,ProcessDataThread 线程读出listSearchLines 中的文字,显示在主界面UI的richTextBox1对象中,运行一段时间,就会出现richTextBox 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
请问我的问题错在哪里?
Thread ProcessDataThread = new Thread((ThreadStart)ProcessData);
ProcessDataThread.IsBackground = true;
ProcessDataThread.Start();
private static List<string> listSearchLines = new List<string>();
private void ProcessData()
{
while (true)
{
try
{
StringBuilder tsb = new StringBuilder();
string[] temp = null;
lock (listSearchLines)
{
temp = listSearchLines.ToArray();
listSearchLines.Clear();
}
if (temp.Length == 0)
{
Thread.Sleep(10);
continue;
}
foreach (string s in temp)
{
tsb.AppendLine(s);
}
this.Invoke((EventHandler)delegate
{
if (richTextBox1.Text.Length > 5000)
{
richTextBox1.Clear();
}
richTextBox1.AppendText(tsb.ToString());
richTextBox1.Focus();
richTextBox1.Select(richTextBox1.Text.Length, 0);
richTextBox1.ScrollToCaret();
richTextBox1.HideSelection = true;
});
tsb.Clear();
}
catch
{
}
}
}