C# - 装入大量文件到一个WPF的RichTextBox? [英] C# - Loading a large file into a WPF RichTextBox?

查看:170
本文介绍了C# - 装入大量文件到一个WPF的RichTextBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个10MB〜范围的文本文件加载到一个WPF RichTextBox的,但我目前的code被凝固起来的用户界面。我试图使后台工作做装载,但似乎没有工作也很好无论是。

下面是我的装载code。有没有什么办法来提高其性能?谢谢你。

  //非常适用于小文件只
    私人无效LoadTextDocument(字符串文件名,RichTextBox的RTB)
    {
        就是System.IO.StreamReader objReader =新的StreamReader(文件名);        如果(File.Exists(文件名))
        {
                rtb.AppendText(objReader.ReadToEnd());
        }
        其他rtb.AppendText(错误:未找到文件!);
        objReader.Close();
    }
    //后台工作版本。不工作顺利
    私人无效LoadBigTextDocument(对象发件人,DoWorkEventArgs E)
    {
        BackgroundWorker的工人=发件人为BackgroundWorker的;
        就是System.IO.StreamReader objReader =新的StreamReader(((字符串[])e.Argument)[0]);
        StringBuilder的的某人=新的StringBuilder(。由于性能原因,只显示第一行1500如果您需要查看整个输出,使用外部程序\\ n,5000);            INT bigcount = 0;
            诠释计数= 1;
            而(objReader.Peek()-1个)
            {
                sB.Append(objReader.ReadLine())追加(\\ n);
                算上++;
                如果(计数%100 == 0安培;&安培; bigcount< 15)
                {
                    worker.ReportProgress(bigcount,sB.ToString());                    bigcount ++;
                    sB.Length = 0;
                }
            }
        objReader.Close();
        e.Result =完成;
    }


解决方案

图形控件只是不设计为处理各种数据,仅仅是因为它会变得不可行。即使控制能够处理大字符串相比,滚动条将变得毫无实际用处的整个文本有什么在控制可视那么一点。要查找文本中的特定行你就必须将滑块移动到的最近位置,它可以指定,然后在时间分钟...

滚动一行

不提交您的用户无用的东西是一样的,你应该重新思考如何显示的数据,这样就可以的方式,实际上是可以用做。

I need to load a ~ 10MB range text file into a WPF 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 loading code. Is there any way to improve its performance? Thanks.

    //works well for small files only
    private void LoadTextDocument(string fileName, RichTextBox rtb)
    {
        System.IO.StreamReader objReader = new StreamReader(fileName);

        if (File.Exists(fileName))
        {
                rtb.AppendText(objReader.ReadToEnd());
        }
        else rtb.AppendText("ERROR: File not found!");
        objReader.Close();
    }






    //background worker version. doesnt work well
    private void LoadBigTextDocument(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;
        System.IO.StreamReader objReader = new StreamReader(   ((string[])e.Argument)[0]  );
        StringBuilder sB = new StringBuilder("For performance reasons, only the first 1500 lines are displayed. If you need to view the entire output, use an external program.\n", 5000);

            int bigcount = 0;
            int count = 1;
            while (objReader.Peek() > -1)
            {
                sB.Append(objReader.ReadLine()).Append("\n");
                count++;
                if (count % 100 == 0 && bigcount < 15)
                {
                    worker.ReportProgress(bigcount, sB.ToString());

                    bigcount++;
                    sB.Length = 0;
                }
            }
        objReader.Close();
        e.Result = "Done";
    }

解决方案

Graphical controls just isn't designed to handle that kind of data, simply because it would become unworkable. Even if the control could handle the large string, what's visible in the control is so little compared to the entire text that the scroll bars would become practically useless. To locate a specific line in the text you would have to move the slider to the closest position that it could specify, then scroll a line at a time for minutes...

Instead of submitting your users to something useless like that, you should rethink how you display the data, so that you can do it in a way that would actually be possible to use.

这篇关于C# - 装入大量文件到一个WPF的RichTextBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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