2011-04-17 21 views
7

System.Net.WebClient.DownloadFileAsync() öğesini kullanarak bir dosyayı indiriyorum. Eşzamansız sürümü kullanmanın tek nedeni, indirme işleminin ilerlemesini göstermektir. Kod yürütmem,% 100'e ulaşılmadan önce devam etmeyebilir. Şu anda meşgul bekle (kod bakın) kullanıyorum ama bunu yapmak için daha akıllı bir yol olup olmadığını merak ediyorum.System.Net.WebClient.DownloadFileAsync() öğesinin indirme tamamlandığını kontrol etmek için meşgul beklemekten daha akıllı bir yol var mı?

using(WebClient oWebClient = new WebClient()) 
{ 
    oWebClient.Encoding = System.Text.Encoding.UTF8; 
    long lReceived = 0; 
    long lTotal = 0; 
    // Set up a delegate to watch download progress. 
    oWebClient.DownloadProgressChanged += delegate(object sender, DownloadProgressChangedEventArgs e) 
    { 
     Console.WriteLine(e.ProgressPercentage + "% (" + e.BytesReceived/1024f + "kb of " + e.TotalBytesToReceive/1024f + "kb)"); 
      // Assign to outer variables to allow busy-wait to check. 
     lReceived = e.BytesReceived; 
     lTotal = e.TotalBytesToReceive; 
    }; 
    oWebClient.DownloadFileAsync(new Uri(sUrl + "?" + sPostData), sTempFile); 
    while(lReceived == 0 || lReceived != lTotal) 
    { 
      // Busy wait. 
     Thread.Sleep(1000); 
    } 
} 

cevap

8
using(WebClient oWebClient = new WebClient()) 
{ 
    // use an event for waiting, rather than a Thread.Sleep() loop. 
    var notifier = new AutoResetEvent(false); 

    oWebClient.Encoding = System.Text.Encoding.UTF8; 
    long lReceived = 0; 
    long lTotal = 0; 
    // Set up a delegate to watch download progress. 
    oWebClient.DownloadProgressChanged += delegate(object sender, DownloadProgressChangedEventArgs e) 
    { 
     Console.WriteLine(e.ProgressPercentage + "% (" + e.BytesReceived/1024f + "kb of " + e.TotalBytesToReceive/1024f + "kb)"); 
     // Assign to outer variables to allow busy-wait to check. 
     lReceived = e.BytesReceived; 
     lTotal = e.TotalBytesToReceive; 

     // Indicate that things are done 
     if(lReceived >= lTotal) notifier.Set(); 
    }; 

    oWebClient.DownloadFileAsync(new Uri(sUrl + "?" + sPostData), sTempFile); 

    // wait for signal to proceed 
    notifier.WaitOne(); 
} 
+0

Cevabınızı yayınladığınızda nefret etmeyin _after_ başka biri kabul edildi: P –

+0

Üzgünüz! Kendimi biliyorum. Bazen bir başkasının yanıtladığını düzenlerken bir uyarı verir, fakat her seferinde anlayamıyorum. – Krumelur

+0

Dostumdan özür dileme :-) cevabı aldın ve haklıydı. Bu işler böyledir! –

5

Bir AutoResetEvent kullanın. DownloadFileAsync() çağrısından sonra bir DownloadFileCompleted olay işleyicisinde, onun WaitOne() yöntemindeki Set() yöntemini çağırın.

+0

! Uzun zaman önce kullandım! Teşekkürler! – Krumelur

0
using(WebClient oWebClient = new WebClient()) 
{ 
    // use an event for waiting, rather than a Thread.Sleep() loop. 
    var notifier = new AutoResetEvent(false); 
    oWebClient.Encoding = System.Text.Encoding.UTF8; 
    long lReceived = 0; 
    long lTotal = 0; 

    // Set up a delegate to watch download progress. 
    oWebClient.DownloadProgressChanged += delegate(object sender, DownloadProgressChangedEventArgs e) 
    { 
     Console.WriteLine(e.ProgressPercentage + "% (" + e.BytesReceived/1024f + "kb of " + e.TotalBytesToReceive/1024f + "kb)"); 
     // Assign to outer variables to allow busy-wait to check. 
     lReceived = e.BytesReceived; 
     lTotal = e.TotalBytesToReceive; 
    }; 

    // Set a delegate to watch for when the download is complete 
     oWebClient.OpenReadCompleted += delegate(object sender, OpenReadCompletedEventArgs e) 
    { 
     // Indicate that things are done 
     notifier.Set(); 
    }; 

    oWebClient.DownloadFileAsync(new Uri(sUrl + "?" + sPostData), sTempFile); 

    // wait for signal to proceed 
    notifier.WaitOne(); 
} 

yerine zaman OpenReadCompleted yangınları bilgilendiricisini ayarlamak için OJ cevap @ genişlettik. Bu, dosya indirme sırasında hata yaparsa iş parçacığının askıda kalmasını durduracaktır.

Ref: Ben AutoResetEvent unutabiliriz nasıl WebClient DownloadFileAsync hangs

İlgili konular