WebClient.DownloadProgressChanged 事件

定義

在非同步下載作業成功傳輸部分或全部資料時發生。

public:
 event System::Net::DownloadProgressChangedEventHandler ^ DownloadProgressChanged;
public event System.Net.DownloadProgressChangedEventHandler? DownloadProgressChanged;
public event System.Net.DownloadProgressChangedEventHandler DownloadProgressChanged;
member this.DownloadProgressChanged : System.Net.DownloadProgressChangedEventHandler 
Public Custom Event DownloadProgressChanged As DownloadProgressChangedEventHandler 
Public Event DownloadProgressChanged As DownloadProgressChangedEventHandler 

事件類型

範例

下列程式碼範例示範如何設定 DownloadProgressChanged 事件的事件處理常式。

// Sample call : DownLoadFileInBackground4 ("http://www.contoso.com/logs/January.txt");
void DownLoadFileInBackground4( String^ address )
{
   WebClient^ client = gcnew WebClient;
   Uri ^uri = gcnew Uri(address);

   // Specify a DownloadFileCompleted handler here...

   // Specify a progress notification handler.
   client->DownloadProgressChanged += gcnew DownloadProgressChangedEventHandler( DownloadProgressCallback4 );
   
   client->DownloadFileAsync( uri, "serverdata.txt" );
}

static void DownloadProgressCallback4(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);
}
// Sample call : DownLoadFileInBackground4 ("http://www.contoso.com/logs/January.txt");
public static void DownLoadFileInBackground4(string address)
{
    WebClient client = new WebClient();
    Uri uri = new Uri(address);

    // Specify a DownloadFileCompleted handler here...

    // Specify a progress notification handler.
    client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback4);

    client.DownloadFileAsync(uri, "serverdata.txt");
}

private static void DownloadProgressCallback4(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);
}
' Sample call : DownLoadFileInBackground4 ("http://www.contoso.com/logs/January.txt");
Public Shared Sub DownLoadFileInBackground4(ByVal address As String)

    Dim client As WebClient = New WebClient()

    ' Specify a DownloadFileCompleted handler here...

    '  Specify a progress notification handler.
    AddHandler client.DownloadProgressChanged, AddressOf DownloadProgressCallback4

    Dim uri as Uri = New Uri(address)
    client.DownloadFileAsync(uri, "serverdata.txt")

End Sub

Private Shared Sub DownloadProgressCallback4(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
    ' Displays the operation identifier, and the transfer progress.
    Console.WriteLine("{0}    downloaded {1} of {2} bytes. {3} % complete...", _
    CStr(e.UserState), e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage)
End Sub

備註

每次非同步下載進行時,都會引發此事件。 使用下列任一方法啟動下載時,就會引發此事件。

方法 描述
DownloadDataAsync 從資源下載資料並傳回 Byte 陣列,而不會封鎖呼叫執行緒。
DownloadFileAsync 將資料從資源下載到本機檔案,而不會封鎖呼叫執行緒。
OpenReadAsync 從資源傳回資料,而不封鎖呼叫執行緒。

DownloadProgressChangedEventHandler是這個事件的委派。 類別 DownloadProgressChangedEventArgs 會提供事件處理常式與事件資料。

如需如何處理事件的詳細資訊,請參閱 處理和引發事件

注意

被動 FTP 檔案傳輸一律會顯示進度百分比為零,因為伺服器未傳送檔案大小。 若要顯示進度,您可以覆 GetWebRequest(Uri) 寫虛擬方法,將 FTP 連線變更為使用中:

internal class MyWebClient : WebClientProtocol
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        FtpWebRequest req = (FtpWebRequest)base.GetWebRequest(address);
        req.UsePassive = false;
        return req;
    }
}

適用於