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 

イベントの種類

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

注釈

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

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

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

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

注意

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

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

適用対象