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

注釈

Caution

WebRequestHttpWebRequestServicePointWebClient は廃止されており、新しい開発には使用しないでください。 HttpClient を代わりに使用します。

このイベントは、非同期ダウンロードが進行するたびに発生します。 このイベントは、次のいずれかの方法を使用してダウンロードが開始されたときに発生します。

Method Description
DownloadDataAsync 呼び出し元のスレッドをブロックせずに、リソースからデータをダウンロードし、 Byte 配列を返します。
DownloadFileAsync 呼び出し元のスレッドをブロックせずに、リソースからローカル ファイルにデータをダウンロードします。
OpenReadAsync 呼び出し元のスレッドをブロックせずに、リソースからデータを返します。

DownloadProgressChangedEventHandlerは、このイベントのデリゲートです。 DownloadProgressChangedEventArgs クラスは、イベント データをイベント ハンドラーに提供します。

イベントの処理方法の詳細については、「イベントの 処理と発生」を参照してください。

Note

パッシブ FTP ファイル転送では、サーバーがファイル サイズを送信しなかったため、進行状況の割合は常に 0 になります。 進行状況を表示するには、 GetWebRequest(Uri) 仮想メソッドをオーバーライドして、FTP 接続をアクティブに変更します。

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

適用対象