WebClient.DownloadFileAsync直到AsyncCompletedEventHandler被触发之前



我有一个问题与WebClient,其中WebClient。在AsyncCompletedEventHandler被触发之前,DownloadFileAsync不会报告DownloadProgressChangedEventArgs。

我有一个方法downloadFileAsync实现在一个名为HTTP的类在一个名为Test.dll。它基本上相当于运行WebClient。DownloadFileAsync(new Uri(URL), downloadLocation);

下面是我用来封装它的测试代码。

Program.cs只是这样做:

namespace DL1
{
    class Program
    {
        static void Main(string[] args)
        {
            FLFront start = new FLFront();
        }
    }
}

FLFront.cs是:

using System;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Timers;
using Test;
namespace DL1
{
    class FLFront
    {
        static HTTP dlMGR;
        static Boolean downloadingCurrently = false;
        static System.Timers.Timer canDLChecker = new System.Timers.Timer();
        public FLFront()
        {
            SpecialWebClient swc = new SpecialWebClient();
            swc.setAmazonArt();
            dlMGR = new HTTP(swc);
                dlMGR.downloadJobs.Enqueue(@"http://downloads.sourceforge.net/project/sevenzip/7-Zip/9.20/7z920-x64.msi?r=http%3A%2F%2Fwww.7-zip.org%2Fdownload.html&ts=1325195085&use_mirror=superb-dca2");
                //dlMGR.downloadJobs.Enqueue(@"http://www.ubuntu.com/start-download?distro=desktop&bits=64&release=latest");
                dlMGR.downloadJobs.Enqueue(@"http://ftp.heanet.ie/mirrors/damnsmalllinux.org/current/dsl-4.4.10-initrd.iso");
                dlMGR.swc.DownloadFileCompleted += new AsyncCompletedEventHandler(dlCompleted);
                dlMGR.swc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(dlProgress);

                canDLChecker.Elapsed += new ElapsedEventHandler(downloadFile);
                canDLChecker.Interval = 500;
                canDLChecker.Enabled = true;
                Thread.Sleep(3000000);
            }
            void dlCompleted(object sender, AsyncCompletedEventArgs e)
            {
                downloadingCurrently = false;
                if (File.Exists(dlMGR.downloadLocation))
                {
                    FileInfo dlInfo = new FileInfo(dlMGR.downloadLocation);
                    Console.WriteLine("Received Size: " + dlInfo.Length.ToString() + " Bytes");
                    if (dlInfo.Length == dlMGR.responseContentLength)
                    {
                        //downloadLocation.ren
                        //Console.WriteLine("Error: This file was not downloaded for some reason");
                        Console.WriteLine("File successfully downloaded.");
                        File.Move(dlMGR.downloadLocation, dlMGR.downloadLocation.Replace(".part", ""));
                    }
                    else
                    {
                        Console.WriteLine("File size mismatch.");
                    }
                }
                else
                    Console.WriteLine("File doesn't exist");
            }
            void dlProgress(object sender, DownloadProgressChangedEventArgs e)
            {
                //Console.WriteLine(e.BytesReceived);
                Console.WriteLine(e.ProgressPercentage + "%  ");
            }
            void downloadFile()
            {
                if (!downloadingCurrently)
                {
                    if (dlMGR.downloadJobs.Count > 0)
                    {
                        downloadingCurrently = true;
                        Console.WriteLine("Download starting: " + dlMGR.downloadJobs.Peek());
                        StringBuilder dlResult = dlMGR.downloadFileAsync(dlMGR.downloadJobs.Dequeue(), @"C:", LoggingLevel.ErrorsOnly);
                        Console.Write(dlResult.ToString());
                        if (dlResult.ToString().Contains("Error: "))
                            dlFailed();
                    }
                    else
                    {
                        Console.WriteLine("No more download jobs exist. Exiting now.");
                        Environment.Exit(0);
                    }
                }
            }
            void downloadFile(object source, ElapsedEventArgs e)
            {
                downloadFile();
                //Console.WriteLine("Check Occurred");
            }
            void dlFailed()
            {
                downloadingCurrently = false;
                Console.WriteLine("Download Failed.");
            }
        }
    }

我现在只在那里有一些测试文件,但是代码没有报告任何进展,直到文件基本上完成下载。

关于为什么不报告进度的想法?

在文件排队等待下载之前进行事件分配。此外,删除Thread.Sleep行,它会停止您的当前线程,并且您的处理程序不会被击中,因为它们是在同一线程中创建的。

相关内容

  • 没有找到相关文章

最新更新