为StreamReader添加进度条监控?

时间:2016-05-20 10:40:24

标签: c# winforms progress-bar

我在Visual Studio 2015中编写了一个非常简单的Windows窗体C#应用程序。这是我的处理程序之一:

private void button2_Click(object sender, EventArgs e)
{
    listBoxCodes.Items.Clear();
    listBoxDuplicates.Items.Clear();

    Cursor.Current = Cursors.WaitCursor;
    Application.DoEvents();

    progressBar.Value = 0;
    using (var reader = new StreamReader(textBoxGENIO.Text))
    {
        // progressBar is set for 5 unit intervals (from 0 to 100)

        // How can I show % complete reading the file?

        string line;
        while ((line = reader.ReadLine()) != null)
        {
            if(line.Length > 8 && line.Substring(0, 4) == "080,")
            {
                string strCode = line.Substring(4, 4);

                if (listBoxCodes.FindStringExact(strCode) == -1)
                    listBoxCodes.Items.Add(strCode);
                else
                    listBoxDuplicates.Items.Add(strCode);
            }
        }
    }

    Cursor.Current = Cursors.Default;
}

现在,我知道我可以通过一次读取文件,获取总行数,然后再次读取文件,并根据行数中的百分比进行进度监控。

但我想避免两次阅读文件,因为它们会变得很大。

是否有任何方法可以使用文件位置/文件大小来确定完成百分比?

我在 StreamReader 对象中看不到任何位置方法。

谢谢。

更新:目前正在查看这个可能是答案的问题:

Tracking the position of the line of a streamreader

更新

我的计算似乎错了:

private void button2_Click(object sender, EventArgs e)
{
    listBoxCodes.Items.Clear();
    listBoxDuplicates.Items.Clear();

    Cursor.Current = Cursors.WaitCursor;
    Application.DoEvents();

    int iLastPercentage = -1;

    progressBar.Value = 0;
    using (var reader = new StreamReader(textBoxGENIO.Text))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            // progressBar is set for 5 unit intervals (from 0 to 100)

            // How can I show % complete reading the file?
            int iPercentage = Convert.ToInt32(((double)reader.BaseStream.Length / 100.0) * (double)reader.BaseStream.Position);
            if (iLastPercentage == -1 || (iPercentage - iLastPercentage >= 5))
            {
                progressBar.PerformStep();
                iLastPercentage = iPercentage;
                Application.DoEvents();
            }

            if (line.Length > 8 && line.Substring(0, 4) == "080,")
            {
                string strCode = line.Substring(4, 4);

                if (listBoxCodes.FindStringExact(strCode) == -1)
                    listBoxCodes.Items.Add(strCode);
                else
                    listBoxDuplicates.Items.Add(strCode);
            }
        }
    }

    Cursor.Current = Cursors.Default;
}

}

2 个答案:

答案 0 :(得分:3)

看看下面是否适合您:

private void button2_Click(object sender, EventArgs e)
{
    listBoxCodes.Items.Clear();
    listBoxDuplicates.Items.Clear();

    Cursor.Current = Cursors.WaitCursor;
    Application.DoEvents();

    progressBar.Value = 0;
    using (var reader = new StreamReader(textBoxGENIO.Text))
    {
        // progressBar is set for 5 unit intervals (from 0 to 100)

        // How can I show % complete reading the file?
        Stream baseStream = reader.BaseStream;
        long length = baseStream.Length;

        string line;
        while ((line = reader.ReadLine()) != null)
        {
            if(line.Length > 8 && line.Substring(0, 4) == "080,")
            {
                string strCode = line.Substring(4, 4);

                if (listBoxCodes.FindStringExact(strCode) == -1)
                    listBoxCodes.Items.Add(strCode);
                else
                    listBoxDuplicates.Items.Add(strCode);
            }

            progressBar.Value = baseStream.Position / length * 100;
            // Add code to update your progress bar UI
        }
    }

    Cursor.Current = Cursors.Default;
}

答案 1 :(得分:1)

这是正确的方法,你必须将它强制转换为双倍

private void button_Load_Int_Click(object sender, EventArgs e)
    {
        Application.DoEvents();
        progressBar.Value = 0;
        using (StreamReader sr = new StreamReader(pathInt))
        {
            Stream baseStream = sr.BaseStream;
            long length = baseStream.Length;
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                string strCode = line.Substring(4, 4);

                if (listBoxCodes.FindStringExact(strCode) == -1)
                    listBoxCodes.Items.Add(strCode);
                else
                    listBoxDuplicates.Items.Add(strCode)
                progressBar.Value = Convert.ToInt32((double)baseStream.Position / length * 100);
                Application.DoEvents();
            }
        }
    }