C#ListBox从文本文件崩溃导入

时间:2012-11-15 20:15:04

标签: c# import crash listbox

我正在使用此代码将文本文件导入我的ListBox

        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Filter = "Text Files|*.txt";
        openFileDialog1.Title = "Select a Text file";
        openFileDialog1.FileName = "";
        DialogResult result = openFileDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            string file = openFileDialog1.FileName;

            string[] text = System.IO.File.ReadAllLines(file);
            foreach (string line in text)
            {
                listBox2.Items.Add(line);

            }
            listBox2.Items.Add("");
        }

它适用于小文本文件,有10行左右,但是当我尝试导入更大的列表(4-5兆字节)时,程序没有响应而且它正在崩溃。

任何帮助?

5 个答案:

答案 0 :(得分:2)

使用C#中的BufferedStream类来提高性能 http://msdn.microsoft.com/en-us/library/system.io.bufferedstream.aspx

答案 1 :(得分:1)

使用这个:

string[] text = System.IO.File.ReadAllLines(file);
listBox1.Items.AddRange(text);

而不是:

string[] text = System.IO.File.ReadAllLines(file);
foreach (string line in text)
{
       listBox2.Items.Add(line);
}

您将加速执行至少10-15次,因为您没有在每个Item插入上使listBox无效。 我已经测量了几千行。

如果你的文字行太多,瓶颈也可能是ReadAllLines。即使我无法弄清楚为什么要插入这么多行,用户是否能够找到他/她需要的行?

编辑好的,我建议你使用BackgroundWorker,这里是代码:

首先初始化BackGroundWorker:

 BackgroundWorker bgw;
        public Form1()
        {
            InitializeComponent();
            bgw = new BackgroundWorker();
            bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
            bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
        }

然后用你的方法调用它:

private void button1_Click(object sender, EventArgs e)
        {
            if (!bgw.IsBusy)
            {
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.Filter = "Text Files|*.txt";
                openFileDialog1.Title = "Select a Text file";
                openFileDialog1.FileName = "";
                DialogResult result = openFileDialog1.ShowDialog();
                if (result == DialogResult.OK)
                {
                    string file = openFileDialog1.FileName;
                    listView1.BeginUpdate();
                    bgw.RunWorkerAsync(file);
                }
            }
            else
                MessageBox.Show("File reading at the moment, try later!");
        }


        void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            listView1.EndUpdate();
        }
        void bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            string fileName = (string)e.Argument;
            TextReader t = new StreamReader(fileName);
            string line = string.Empty;
            while ((line = t.ReadLine()) != null)
            {
                string nLine = line;
                this.Invoke((MethodInvoker)delegate { listBox1.Items.Add(nLine); });
            }
        }

它会在读取时添加每一行,您将拥有响应式UI,并且在完成加载之前行不会影响listBox。

答案 2 :(得分:0)

可以使用流来存储数据:

class Test
{

public static void Main()
{
    string path = @"c:\temp\MyTest.txt";

    //Create the file. 
    using (FileStream fs = File.Create(path))
    {
        AddText(fs, "This is some text");
        AddText(fs, "This is some more text,");
        AddText(fs, "\r\nand this is on a new line");
        AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n");

        for (int i=1;i < 120;i++)
        {
            AddText(fs, Convert.ToChar(i).ToString());

        }
    }

    //Open the stream and read it back. 
    using (FileStream fs = File.OpenRead(path))
    {
        byte[] b = new byte[1024];
        UTF8Encoding temp = new UTF8Encoding(true);
        while (fs.Read(b,0,b.Length) > 0)
        {
            Console.WriteLine(temp.GetString(b));
        }
    }
}

private static void AddText(FileStream fs, string value)
{
    byte[] info = new UTF8Encoding(true).GetBytes(value);
    fs.Write(info, 0, info.Length);
}

}

然后是你的事件处理程序

 privateasyncvoid Button_Click(object sender, RoutedEventArgs e)
    {
        UnicodeEncoding uniencoding = new UnicodeEncoding();
        string filename = @"c:\Users\exampleuser\Documents\userinputlog.txt";

        byte[] result = uniencoding.GetBytes(UserInput.Text);

        using (FileStream SourceStream = File.Open(filename, FileMode.OpenOrCreate))
        {
            SourceStream.Seek(0, SeekOrigin.End);
            await SourceStream.WriteAsync(result, 0, result.Length);
        }
    }

答案 3 :(得分:0)

它可能根本就没有完成它的工作,你应该等待更多。试试这个解决方案:

http://www.bytechaser.com/en/articles/f3a3niqyb7/display-large-lists-in-listview-control-quickly.aspx

答案 4 :(得分:0)

您的应用程序无响应,因为它正在等待ReadAllLines方法完成并阻止UI线程。您可能希望在单独的线程上读取文件以避免阻止UI。我不能保证下面的代码可以正常工作,但它应该让你知道如何解决这个问题。

首先,您需要一种方法将项目附加到ListBox

private void AddListBoxItem(string item)
{
    if(!InvokeRequired)
    {
        listBox2.Items.Add(item);
    }
    else
    {
        var callback = new Action<string>(AddListBoxItem);
        Invoke(callback, new object[]{item});
    }
}

上面的方法检查它是否在UI线程上执行,如果是,它只是将一个项添加到listBox2.Items集合中;如果没有,它会从自身创建一个委托,并在UI线程上调用该委托。

接下来,您需要将读取文件的代码移动到另一个线程并调用AddListBoxItem方法。为了便于阅读,我们将其放入一个单独的方法中:

private void AddFileContentsToList(string fileName)
{
    using(var reader = new System.IO.StreamReader(fileName))
    {
        while(!reader.EndOfStream)
        {
            var line = reader.ReadLine();
            AddListBoxItem(line);
        }
    }
}

现在我们将在一个单独的线程上调用该方法:

OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Text Files|*.txt";
openFileDialog1.Title = "Select a Text file";
openFileDialog1.FileName = "";
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
    var thread = new Thread(AddFileContentsToList);
    thread.Start();
}

希望这有帮助!