简体   繁体   English

DownloadFileCompleted / DownloadProgressChanged事件不起作用

[英]DownloadFileCompleted/DownloadProgressChanged events do not work

I have a background worker (as seen below) with DownloadFileCompleted/DownloadProgressChanged events. 我有一个背景工作人员(如下所示),具有DownloadFileCompleted / DownloadProgressChanged事件。 After several hours of testing and a lot of research, I couldn't get it to work. 经过数小时的测试和大量研究,我无法使其正常工作。 The file downloads successfully but the events don't run. 文件下载成功,但事件未运行。 I looked at a lot of documentation pages and searched for this issue here, but they don't seem to match my case... Would appreciate if someone could help me out on this one! 我查看了很多文档页面并在此处搜索了此问题,但是它们似乎与我的情况不符...如果有人可以帮助我解决这一问题,将不胜感激!

    private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
            WebClient client = new WebClient();
            client.DownloadProgressChanged += new DownloadProgressChangedEventHandler (webClient_DownloadProgressChanged);
            client.DownloadFileCompleted += new AsyncCompletedEventHandler  (webClient_DownloadFileCompleted);

            client.DownloadFileAsync(new Uri(myUri), path);

        }
    }

    void webClient_DownloadFileCompleted(object s, AsyncCompletedEventArgs e)
    {
        Debug.WriteLine("Download completed!");
    }
    void webClient_DownloadProgressChanged(object s, DownloadProgressChangedEventArgs e)
    {
        Debug.WriteLine(e.BytesReceived);
    }

There must be something else wrong with your code as running effectively that same code, it works fine. 您的代码一定存在其他问题,因为该代码可以有效地运行,所以效果很好。

Are you sure your program is simply not finished and closing before the work is done as a BackgroundWorker won't keep your application alive. 您确定您的程序只是没有完成并在完成工作之前关闭,因为BackgroundWorker不会使您的应用程序保持活动状态。

Here is my example, with a foreground Thread to keep the application alive until done 这是我的示例,带有一个前台线程来保持应用程序运行直到完成

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Net;
using System.Threading;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            new Thread(() =>
            {
                var client = new WebClient();
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
                client.DownloadFileCompleted += new AsyncCompletedEventHandler(webClient_DownloadFileCompleted);

                client.DownloadFileAsync(new Uri("https://speed.hetzner.de/100MB.bin"), @"C:\Users\Luke\Desktop\100mb.bin");
            }).Start();
        }

        private static void webClient_DownloadFileCompleted(object s, AsyncCompletedEventArgs e)
        {
            Debug.WriteLine("Download completed!");
        }
        private static void webClient_DownloadProgressChanged(object s, DownloadProgressChangedEventArgs e)
        {
            Debug.WriteLine(e.BytesReceived);
        }
    }
}

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

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