阅读Stream时,Progressbar不起作用

时间:2013-03-19 16:04:17

标签: c# backgroundworker streamreader

我已经看到很多关于进度条和多线程的问题和帖子,所以我希望我不会问一个其他地方已经回答过的问题。但是,如果我这样做,我的appologies,我试图找到一个解决方案。

我有一些大文件,其中我想替换&由&因为这些文件很大,所以我希望每隔一段时间看一次statusupdate,然后查看文档解析的距离。我编写的代码用于替换&符号,但是,只有在解析文档之后才会进行进度更新。任何想法有什么不对?

using System;
using System.Windows;
using Microsoft.Win32;
using System.Text;
using System.Xml;
using System.IO;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Data;
using System.Text.RegularExpressions;
using System.ComponentModel;

namespace DigiPort
{
    public partial class MainWindow : Window
    {

        public string filename;
        public string xmlfilename;
        BackgroundWorker worker = new BackgroundWorker();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void onImportDocClicked(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Text documents (.txt)|*.txt";
            Nullable<bool> result = ofd.ShowDialog();
            if (result == true)
            {
                OutPutWindow.Text = "Document wordt nu gelezen.";
                filename = ofd.FileName;
                worker.DoWork += new DoWorkEventHandler(ReplaceAmpersandAsync);
                worker.ProgressChanged += new ProgressChangedEventHandler(ProBarChanged);
                worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(ReplaceAmpersandCompleted);
                worker.WorkerReportsProgress = true;
                worker.ReportProgress(0, "Start counting lines");
                int count = ReadNumberOfLines();
                worker.ReportProgress(0, "Lines counted. Total number of lines is: " + count);
                worker.RunWorkerAsync(count);
                OutPutWindow.Text = "Document gelezen en ampersands vervangen.";
            }
        }

        private int ReadNumberOfLines()
        {
            int count = 0;
            using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            using (BufferedStream bs = new BufferedStream(fs))
            using (StreamReader sr = new StreamReader(bs))
                while (!sr.EndOfStream)
                {
                    sr.ReadLine();
                    count++;
                }
            return count;
        }

        private void ReplaceAmpersandAsync(object sender, DoWorkEventArgs e)
        {
            int i = 0;
            int count = (int)e.Argument;
            xmlfilename = filename + ".xml";
            StreamWriter writer = new StreamWriter(xmlfilename);
            using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            using (BufferedStream bs = new BufferedStream(fs))
            using (StreamReader sr = new StreamReader(bs))
            while (!sr.EndOfStream)
            {
                double percentage = (i++ * 100) / count;
                worker.ReportProgress((int)Math.Round(percentage, 0));
                if (i % 100000 == 0)
                {
                    worker.ReportProgress(i, "Linenumber " + i + " is now parsed");
                }
                string content = sr.ReadLine();
                writer.WriteLine(content.Replace("&", "&amp;"));
            }

            writer.Close();
            worker.ReportProgress(i, "All lines are parsed, file is saved");
        }

        private void ProBarChanged(object sender, ProgressChangedEventArgs e)
        {
            ProBar.Value = e.ProgressPercentage;
            OutPutWindow.Text += (string)e.UserState;
        }

        private void ReplaceAmpersandCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
        }
    }
}

1 个答案:

答案 0 :(得分:3)

你也筹集了太多的活动。您不应该为每个被解析的行引发事件。这将填充Windows消息队列,它将无法跟上。只有在每次取得重大进展时才举起活动(例如每1%完成一次活动)。

ReportProgress的第一个参数应该是0到100之间的百分比,表示工作的完整程度。你在这里滥用它来返回一个行号:

worker.ReportProgress(i, "Linenumber " + i + " is now parsed");

还值得一提的是,您目前正在吞噬代码可能产生的任何异常。在完成的事件处理程序中,您应该检查是否有错误:

private void ReplaceAmpersandCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (e.Error != null) {
        // Show the user an error message.
    }
}