读取流时进度条不工作
本文关键字:工作 读取 | 更新日期: 2023-09-27 18:04:37
我已经看到了很多关于进度条和多线程的问题和帖子,所以我希望我没有问一个在其他地方已经回答的问题。但是,如果我做了,我的道歉,我试图找到一个解决方案。
我有一些大文件,我想替换&由,因为这些文件很大,所以我希望不时看到状态更新,看看文档已经解析了多少。我所编写的代码用于替换&号,但是,只有在文档被解析后才会进行进度更新。知道怎么回事吗?
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("&", "&"));
}
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)
{
}
}
}
您也引发了太多事件。不应该为解析的每一行都引发事件。这将填满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.
}
}