利用线程把文本文件填充到richTextBox;防止导入大文本文件窗口假死现象
private void btnDr_Click(object sender, EventArgs e) { richTextBox1.Text = ""; //richTextBox控件 labelPercent.Visible = true; //显示当前行数的label string path = ""; OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "文件文件(*.txt)|*.txt"; if (openFileDialog1.ShowDialog() != DialogResult.OK) return; path = openFileDialog1.FileName; new Thread((ThreadStart)delegate { StreamReader sr = new StreamReader(openFileDialog1.FileName, Encoding.GetEncoding("gb2312")); int line = 0; int lineCount = line + 1; string strCount = ""; string strline = sr.ReadLine(); while (strline != "" && strline != null && !strline.Equals("")) { this.Invoke((EventHandler)delegate { this.labelPercent.Text = "已经读取 " + lineCount.ToString() + " 行"; }); this.richTextBox1.Text = this.richTextBox1.Text + strline + "\r\n"; strline = sr.ReadLine(); line++; lineCount++; } sr.Close(); }).Start(); }