c# richtextbox更新大量数据不卡死的实现方式

  maxDisplayline = 1 * 1000; // 1000行

  StringBuilder sb = new StringBuilder();

  Stopwatch swGlobal = new Stopwatch();

  string[] sblineslist = in_str.Split(new char[] { '

  ' }); // instr 为输入字符串,可以是文件读入的

  // static readonly object lockSb = new object();

  richTextBoxDisplay.Focus();

  sb.Clear();

  Thread.Sleep(1);

  swGlobal.Reset(); swGlobal.Start();

  Thread t = new Thread((ThreadStart)delegate

  {

  try

  {

  for (int i = 0; i < sblineslist.Length; i++)

  {

  if (stopSign)

  {

  return;

  }

  // lock(lockSb)

  sb.Append(sblineslist[i] + "

  ");

  if (i > 0 && i % maxDisplayline == 0)

  {

  this.Invoke((EventHandler)delegate { labelStatus.Text = "状态: " + count + "/" + manualSyncFilesFullname.Count + " " + runsecond + "s -> " + (i + 1)

  + " Act/Rest: " + swGlobal.ElapsedMilliseconds/1000 + "/" + (int)(1.0 * swGlobal.ElapsedMilliseconds * (sblineslist.Length - (i+1)) /(i+1) /1000) + "s";

  labelStatus.Update(); });

  if (sb.Length > 0)

  {

  // lock(lockSb)

  this.richTextBoxDisplay.Text = this.richTextBoxDisplay.Text + sb.ToString();

  sb.Clear();

  }

  maxDisplayline = maxDisplayline * ((int)Math.Sqrt(i/ maxDisplayline));

  }

  }

  if (sb.Length > 0)

  {

  // lock(lockSb)

  this.richTextBoxDisplay.Text = this.richTextBoxDisplay.Text + sb.ToString();

  sb.Clear();

  }

  this.Invoke((EventHandler)delegate { labelStatus.Text = "状态: " + count + "/" + manualSyncFilesFullname.Count + " " + runsecond + "s -> " + sblineslist.Length.ToString()

  +" Act: " + swGlobal.ElapsedMilliseconds / 1000 + "s";

  labelStatus.Update(); });

  }

  catch (Exception ex)

  {

  try

  {

  stopSign = true;

  this.Invoke((EventHandler)delegate { labelStatus.Text = "异常错误: " + ex.Message; labelStatus.Update(); });

  }

  catch { }

  }

  });

  t.IsBackground = true;

  t.Start();

  Thread.Sleep(33);

  while (!stopSign && t != null && t.IsAlive)

  {

  Application.DoEvents();

  Thread.Sleep(3);

  }