将线程中的文本文件读取到面板内的 RichtextBox [英] Reading a text file in Threads to RichtextBox inside Panel

查看:50
本文介绍了将线程中的文本文件读取到面板内的 RichtextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Windows 窗体应用程序,我在其中创建了一个 Panel.I 设置了 Panel True.Inside Panel 的 AutoScroll 属性我创建了一个 RichtextBox.Now 根据我的要求,我必须逐行读取文本文件在每行延迟到这个 RichtextBox.我已经在 frmHome_Load 事件中使用计时器来做到这一点.我有以下代码来做到这一点..

I have a windows Form application in which i have created a Panel.I have set the AutoScroll property of Panel True.Inside Panel i have created a RichtextBox.Now as per my requirement i have to read a text file line by line with delay in each line into this RichtextBox.I have taken timer to do this in frmHome_Load event.I have the following code to do this ..

private void frmHome_Load(object sender, EventArgs e)
    {

        timer1.Interval = 1000;
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        const Int32 BufferSize = 128;
        using (var fileStream = File.OpenRead("E:\\File\\temp.txt"))
        using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true, BufferSize))
        {
            String line;
            while ((line = streamReader.ReadLine()) != null)
            {
                // Process line
                richTextBox1.Text += line + Environment.NewLine;

            }

        }
    }

现在使用此代码,文本文件很大,它使我的 Windows 窗体应用程序挂起.意味着除了从任务栏关闭窗口之外,我无法做任何事情.

Now with this code as,the text file is large it is making my windows Form application hanged.Means I am not able to do any thing except closing of the window from the Task Bar.

为了解决这个问题,我想使用线程来读取和显示文本文件的文本.使用线程我想每 5 秒睡眠一次,然后从那里重新开始.我想知道这是正确的方式并将解决我的问题.如果是,如何使用线程对我发布的代码执行相同的操作.请帮忙.

To come out of this i thought to use Threads to read and display the texts of the textfile.With Threads i want to sleep with every 5 seconds and re starts from there. I want to know that is this way Correct and will resolve my issue.If yes How to use thread to do same with my posted Code. Please Help.

推荐答案

如果您使用的是 .Net 4.5 或更高版本,您可以轻松地执行以下操作:

If you are using .Net 4.5 or later, you can easily do this as follows:

protected async override void OnLoad(EventArgs e)
{
    base.OnLoad(e);

    string filename = "E:\\File\\temp.txt";
    int delayTimeMilliseconds = 5000;

    // Note: File.ReadLines() defaults to UTF8.

    foreach (string line in File.ReadLines(filename))
    {
        await Task.Delay(delayTimeMilliseconds);
        richTextBox1.AppendText(line + "\n");
    }
}

这是一个 .Net 4.0 解决方案:

Here's a .Net 4.0 solution:

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    Task.Factory.StartNew(loadTextLineByLine);
}

private void loadTextLineByLine()
{
    string filename = "E:\\File\\temp.txt";
    int delayTimeMilliseconds = 5000;

    foreach (string line in File.ReadLines(filename))
    {
        Thread.Sleep(delayTimeMilliseconds);
        string text = line; // Prevent modified closure.
        this.BeginInvoke(new Action(() => richTextBox1.AppendText(text + "\n")));
    }
}

这篇关于将线程中的文本文件读取到面板内的 RichtextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆