WPF的BackgroundWorker进度栏遇到的困难

编程入门 行业动态 更新时间:2024-10-24 08:19:18
本文介绍了WPF的BackgroundWorker进度栏遇到的困难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发WPF,C#应用程序,我必须读取一个很大的CSV文件并写入数据库.这个过程需要很长时间,所以我至少要显示一个进度条,随着进度的临近,进度条的大小会逐渐增加.

I am developing a WPF, C# application and I have to read a very big CSV file and write to a database. This process takes a long time so I want to at least show a progress bar that grows in size as it nears completition.

现在,我在顶部有以下代码:

Now, I have the following code at the top:

private readonly BackgroundWorker worker = new BackgroundWorker();

然后在循环中,我有这个:

Then inside the loop I have this:

worker.WorkerReportsProgress = true; worker.ReportProgress((100 * i) / 10000);

并且我有一个像这样的私人潜艇:

and I have a private sub like this:

private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) { prgBar.Value = Math.Min(e.ProgressPercentage, 100); }

但是,我没有看到进度条更新或发生任何事情,程序仍然挂起.我在做错什么吗?

Yet I don't see the progress bar updating or anything happening, program still hangs. Am I doing something wrong?

更新:在此处尝试了MSDN指南 http ://msdn.microsoft/zh-cn/library/cc221403%28v=vs.95%29.aspx ,但我不知何故无法在该示例中使用它.我当然调整了循环部分.嘿.它仍然保持空闲状态,没有任何更新.哎呀,我要做的就是得到一个小的计数器,该计数器在每次读取和添加一行时都会增加.

UPDATE: Tried the MSDN guide here msdn.microsoft/en-us/library/cc221403%28v=vs.95%29.aspx and I somehow failed to get it working with that example. I adjusted the looping part of course. Heh. It still stays idle and nothing updates. Heck, all I want to do is get a small counter that increases every time a line is read and added.

更新的代码:

private BackgroundWorker bw = new BackgroundWorker(); public Process() { bw.WorkerReportsProgress = true; bw.WorkerSupportsCancellation = true; bw.DoWork += new DoWorkEventHandler(bw_DoWork); bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged); bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted); } private void bw_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; // Perform a time consuming operation and report progress. int d = 0; Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); // Set filter for file extension and default file extension dlg.DefaultExt = ".csv"; dlg.Filter = "CSV File (*.csv)|*.csv"; dlg.Title = "file"; // Display OpenFileDialog by calling ShowDialog method Nullable<bool> result = dlg.ShowDialog(); if (result == true) { // Open document string filename = dlg.FileName; List<String[]> fileContent = new List<string[]>(); using (FileStream reader = File.OpenRead(@filename)) // mind the encoding - UTF8 using (TextFieldParser parser = new TextFieldParser(reader)) { using (SqlConnection conn = new SqlConnection(Classes.PublicVariables.Connection)) { parser.Delimiters = new[] { "," }; parser.HasFieldsEnclosedInQuotes = true; while (!parser.EndOfData) { string[] line = parser.ReadFields(); fileContent.Add(line); SqlCommand comm = QUERYANDPARAMETERS d += 1; comm.ExecuteNonQuery(); worker.ReportProgress((d * 10)); } } } } private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { lblCount.Content = "Complete"; } private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e) { this.lblCount.Content = (e.ProgressPercentage.ToString() + "%"); }

当工人奔跑时,我感到非常高兴.

I am getting an excerption when the worker runs.

An exception of type 'System.NullReferenceException' occurred inSolution.exe but was not handled in user code

其他信息:对象引用未设置为对象的实例.

Additional information: Object reference not set to an instance of an object.

UPDATE3

让它正常工作!我需要的是与上面相同的代码,但这是

Got it working! All I needed was the same code as above but this:

if (bw.IsBusy != true) { bw.RunWorkerAsync(); } else { bw_DoWork(null, null); }

在事件处理程序中按下按钮.

In the event handler for the button press.

推荐答案

这不会按原样编译,但应该使您朝正确的方向开始

This will not compile as-is, but should get you started in the right direction:

private readonly BackgroundWorker worker = new BackgroundWorker { WorkerReportsProgress = true }; public MainWindow() { InitializeComponent(); worker.DoWork += worker_DoWork; worker.ProgressChanged += worker_ProgressChanged; } private void worker_DoWork(object sender, DoWorkEventArgs doWorkEventArgs) { // Do some long process, break it up into a loop so you can periodically // call worker.ReportProgress() worker.ReportProgress(i); // Pass back some meaningful value } private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) { prgBar.Value = Math.Min(e.ProgressPercentage, 100); }

更多推荐

WPF的BackgroundWorker进度栏遇到的困难

本文发布于:2023-11-27 11:59:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637979.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进度   困难   WPF   BackgroundWorker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!