读书人

c#读取大容量txt文件如何才能不卡

发布时间: 2013-12-21 20:16:01 作者: rapoo

c#读取大容量txt文件怎么才能不卡
c#读取大容量txt文件怎么才能不卡还请高手指教,在线等,谢谢了,
[解决办法]
多线程


//例子
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace MultiThread
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog fbd = new FolderBrowserDialog())
{
fbd.Description = "选择要多线程读取文件的路径";
fbd.ShowNewFolderButton = false;
if (fbd.ShowDialog(this) == DialogResult.OK)
{
DirectoryInfo di = new DirectoryInfo(fbd.SelectedPath);
foreach (FileInfo fi in di.GetFiles("*.txt"))
{
Thread t = new Thread(this.InvokeThread);
t.Start(fi.FullName);
}
}
}
}
private delegate void ReadFile(object filePath);
private void InvokeThread(object filePath)
{
if (this.InvokeRequired)
{
this.Invoke(new ReadFile(ReadFileContent), filePath);
}
else
{
ReadFileContent(filePath);
}
}
private void ReadFileContent(object filePath)
{
this.textBox1.AppendText(File.ReadAllText(filePath.ToString(), Encoding.Default));
this.textBox1.AppendText("\r\n");
}
}
}

读书人网 >C#

热点推荐