WebClient.DownloadProgressChanged Evento

Definição

Ocorre quando uma operação de download assíncrona transfere com êxito alguns ou todos os dados.

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 

Tipo de evento

Exemplos

O exemplo de código a seguir demonstra a configuração de um manipulador de eventos para o DownloadProgressChanged evento.

// 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

Comentários

Esse evento é gerado sempre que um download assíncrono avança. Esse evento é gerado quando os downloads são iniciados usando qualquer um dos métodos a seguir.

Método Descrição
DownloadDataAsync Baixa dados de um recurso e retorna uma Byte matriz, sem bloquear o thread de chamada.
DownloadFileAsync Baixa dados de um recurso para um arquivo local, sem bloquear o thread de chamada.
OpenReadAsync Retorna os dados de um recurso, sem bloquear o thread de chamada.

O DownloadProgressChangedEventHandler é o delegado para esse evento. A DownloadProgressChangedEventArgs classe fornece ao manipulador de eventos dados de evento.

Para obter mais informações sobre como lidar com eventos, consulte Manipulando e levantando eventos.

Observação

Uma transferência passiva de arquivo FTP sempre mostrará uma porcentagem de progresso igual a zero, já que o servidor não enviou o tamanho do arquivo. Para mostrar o progresso, você pode alterar a conexão FTP para ativa substituindo o GetWebRequest(Uri) método virtual:

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

Aplica-se a