简体   繁体   English

此 BackgroundWorker 声明它不报告进度

[英]This BackgroundWorker states that it doesn't report progress

Where is my mistake?我的错误在哪里? I think everything is fine but still I see this exception:我认为一切都很好,但我仍然看到这个例外:

Exception {"This BackgroundWorker states that it doesn't report progress. Modify WorkerReportsProgress to state that it does report progress."}.Exception wasthrown in _worker.ReportProgress((int)(progressPercentage*100)).异常 {"此 BackgroundWorker 声明它不报告进度。修改 WorkerReportsProgress 以声明它报告进度。"}.Exception wasthrown in _worker.ReportProgress((int)(progressPercentage*100))。

Can somebody recommend me a ftp server with rich data for test download and upload?有人可以推荐我一个具有丰富数据的 ftp 服务器用于测试下载和上传吗?

Here is my code:这是我的代码:

    this._worker = new BackgroundWorker();

    this._worker.WorkerSupportsCancellation = true;
    this._worker.WorkerSupportsCancellation = true;

    this._worker.ProgressChanged += backgroundWorker1_ProgressChanged;
    this._worker.DoWork += backgroundWorker1_DoWork;

using (FileStream fs = new    FileStream(Path.Combine(this._ftpInfo.SaveDirectory, this._ftpInfo.FileName), FileMode.CreateNew, FileAccess.Write))
                    {


                byte[] buffer = new byte[1024];
                    int len;
                    int byteTotal = 0;

                    while ((len = stream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        await fs.WriteAsync(buffer, 0, len);
                        byteTotal += len;
                        double index = (double)(byteTotal);
                        double total = (double)buffer.Length;
                        double progressPercentage = (index / total);
                        _worker.ReportProgress((int)(progressPercentage*100));

                    }
                    fs.Close();

You have this line twice:你有这行两次:

this._worker.WorkerSupportsCancellation = true;

One of those was probably supposed to be:其中之一可能应该是:

this._worker.WorkerReportsProgress = true;

On a side note, consider using Task.Run with IProgress<T> instead of BackgroundWorker .附带说明一下,请考虑使用Task.RunIProgress<T>而不是BackgroundWorker The resulting code will be shorter and more type-safe.生成的代码将更短且类型更安全。

您必须从对象属性中将 WorkerReportsProgress 设置为 True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM