Bagikan melalui


WebClient.DownloadProgressChanged Kejadian

Definisi

Terjadi ketika operasi pengunduhan asinkron berhasil mentransfer beberapa atau semua data.

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 

Jenis Acara

Contoh

Contoh kode berikut menunjukkan pengaturan penanganan aktivitas untuk peristiwa tersebut 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

Keterangan

Kejadian ini dimunculkan setiap kali unduhan asinkron membuat kemajuan. Kejadian ini dinaikkan ketika unduhan mulai menggunakan salah satu metode berikut.

Metode Deskripsi
DownloadDataAsync Mengunduh data dari sumber daya dan mengembalikan array, tanpa memblokir utas Byte panggilan.
DownloadFileAsync Mengunduh data dari sumber daya ke file lokal, tanpa memblokir utas panggilan.
OpenReadAsync Mengembalikan data dari sumber daya, tanpa memblokir utas panggilan.

DownloadProgressChangedEventHandler adalah delegasi untuk peristiwa ini. Kelas DownloadProgressChangedEventArgs menyediakan penanganan aktivitas dengan data peristiwa.

Untuk informasi selengkapnya tentang cara menangani peristiwa, lihat Menangani dan Menaikkan Peristiwa.

Catatan

Transfer file FTP pasif akan selalu menunjukkan persentase kemajuan nol, karena server tidak mengirim ukuran file. Untuk menampilkan kemajuan, Anda dapat mengubah koneksi FTP menjadi aktif dengan mengambil alih GetWebRequest(Uri) metode virtual:

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

Berlaku untuk