2012-07-17 3 views

답변

6
mWebClient.DownloadProgressChanged += (sender, e) => progressChanged(e.BytesReceived); 
//... 
DateTime lastUpdate; 
long lastBytes = 0; 

private void progressChanged(long bytes) 
{ 
    if (lastBytes == 0) 
    { 
     lastUpdate = DateTime.Now; 
     lastBytes = bytes; 
     return; 
    } 

    var now = DateTime.Now; 
    var timeSpan = now - lastUpdate; 
    var bytesChange = bytes - lastBytes; 
    var bytesPerSecond = bytesChange/timeSpan.Seconds; 

    lastBytes = bytes; 
    lastUpdate = now; 
} 

그리고 bytesPerSecond 변수로 필요한 모든 작업을 수행하십시오.

+0

감사합니다. 훌륭한 작품입니다. – Drahcir

+1

lastUpdate가 동일한 초 내에 있으면 timeSpan.Seconds가 0이어서 0으로 나눕니다. – Chris

0

사용 DownloadProgressChanged event

WebClient client = new WebClient(); 
Uri uri = new Uri(address); 

// Specify that the DownloadFileCallback method gets called 
// when the download completes. 
client.DownloadFileCompleted += new AsyncCompletedEventHandler (DownloadFileCallback2); 
// Specify a progress notification handler. 
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback); 
client.DownloadFileAsync (uri, "serverdata.txt"); 

private static void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e) 
{ 
    // Displays the operation identifier, and the transfer progress. 
    Console.WriteLine("{0} downloaded {1} of {2} bytes. {3} % complete...", 
     (string)e.UserState, 
     e.BytesReceived, 
     e.TotalBytesToReceive, 
     e.ProgressPercentage); 
} 
0

당신은 웹 클라이언트를 연결하면, 당신은 ProgressChanged 이벤트에 subsribe 수 있습니다, 예를 들어, 이 핸들러에 대한

_httpClient = new WebClient(); 
_httpClient.DownloadProgressChanged += DownloadProgressChanged; 
_httpClient.DownloadFileCompleted += DownloadFileCompleted; 
_httpClient.DownloadFileAsync(new Uri(_internalState.Uri), _downloadFile.FullName); 

EventArgs입니다 당신에게 BytesReceieved 및 TotalBytesToReceive을 제공합니다. 이 정보를 사용하여 다운로드 속도를 결정하고 그에 따라 진행률 표시 줄을 촬영할 수 있어야합니다.

관련 문제