2011-12-29 1 views
1

WebClient.DownloadFileAsyncがAsyncCompletedEventHandlerが発生する直前まで、DownloadProgressChangedEventArgsを報告していないWebClientに問題があります。WebClient.DownloadFileAsyncは、AsyncCompletedEventHandlerが発生する直前までDownloadProgressChangedEventArgsを報告しません。

Test.dllという名前のDLLにHTTPというクラスで実装されたメソッドdownloadFileAsyncがあります。 これは基本的にWebClient.DownloadFileAsync(新しいUri(URL)、downloadLocation)を実行することになります。

ここには、私がそれを囲むために使用しているテストコードがあります。

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

FLFront.csこのです:

Program.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."); 
      } 
     } 
    } 

私はちょうど今そこにいくつかのテストファイルを持っていますが、コードはいずれも報告されていませんファイルが基本的にダウンロードされるまで進行します。

なぜそれが進捗状況を報告していないかについての考え方?

+0

どこからダウンロードを開始しますか? 'HTTP'クラスは何をしますか? 'SpecialWebClient'は' WebClient'をどのように実装していますか?そして、なぜあなたはとても長い間その糸を眠っていますか?次のコードは正常に動作します。 'WebClient wc =新しいWebClient(); wc.DownloadProgressChanged + =(送信者、e)=> { }; wc.DownloadFileAsync(新しいUri( "some-file")、@ "d:\ somefile"); ' –

+0

私のテスト効果の古典的なケースのように聞こえます。同じURLでコードを何度も繰り返しテストすることは避けてください。それ以外の場合は、マシンを再起動し、IEキャッシュを消去して、代表的な結果を得てください。 –

+0

HTTPは、WebClientを使いやすくするいくつかのメソッドのフロントエンドクラスに過ぎません。 SpecialWebClientは、WebClientにCookieを保存させる拡張クラスです。 – cvocvo

答えて

0

ダウンロードするファイルをエンキューする前に、イベントの割り当てを行います。また、Thread.Sleep行を削除すると、現在のスレッドが停止し、ハンドラは同じスレッド内で作成されるため、ヒットしません。

+0

これはコンソールアプリケーションなので... thread.sleepが開いたままにすることができました - 今は不要です。イベントハンドラを割り当てる前/後に、HTTP内のキューに追加する必要はありません。 – cvocvo

関連する問題