I need to load a - 10MB range text file into a Winform RichTextBox, but my current code is freezing up the UI. I tried making a background worker do the loading, but that doesnt seem to work too well either.
Here's my several loading code which I was tried. Is there any way to improve its performance? Thanks.
private BackgroundWorker bw1;
private string[] lines;
Action showMethod;
private void button1_Click(object sender, EventArgs e)
{
bw1 = new BackgroundWorker();
bw1.DoWork += new DoWorkEventHandler(bw_DoWork);
bw1.RunWorkerCompleted += bw_RunWorkerCompleted;
string path = @"F:\DXHyperlink\Book.txt";
if (File.Exists(path))
{
string readText = File.ReadAllText(path);
lines = readText.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
bw1.RunWorkerAsync();
}
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
Invoke((ThreadStart)delegate()
{
for (int i = 0; i < lines.Length; i++)
{
richEditControl1.Text += lines[i] + "\n";
}
});
}
I Also Try:
Action showMethod = delegate()
{
for (int i = 0; i < lines.Length; i++)
{
richEditControl1.Text += lines[i] + "\n";
}
};