WebClient.DownloadDataCompleted 事件    
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
异步数据下载操作完成时发生。
public:
 event System::Net::DownloadDataCompletedEventHandler ^ DownloadDataCompleted;
	public event System.Net.DownloadDataCompletedEventHandler? DownloadDataCompleted;
	public event System.Net.DownloadDataCompletedEventHandler DownloadDataCompleted;
	member this.DownloadDataCompleted : System.Net.DownloadDataCompletedEventHandler 
	Public Custom Event DownloadDataCompleted As DownloadDataCompletedEventHandler 
	Public Event DownloadDataCompleted As DownloadDataCompletedEventHandler 
	事件类型
示例
下面的代码示例演示如何为此事件设置事件处理程序。
// Sample call : DownLoadDataInBackground ("http://www.contoso.com/GameScores.html");
void DownloadDataInBackground( String^ address )
{
   System::Threading::AutoResetEvent^ waiter = gcnew System::Threading::AutoResetEvent( false );
   WebClient^ client = gcnew WebClient;
   Uri ^uri = gcnew Uri(address);
   // Specify that the DownloadDataCallback method gets called
   // when the download completes.
   client->DownloadDataCompleted += gcnew DownloadDataCompletedEventHandler( DownloadDataCallback );
   client->DownloadDataAsync( uri, waiter );
   // Block the main application thread. Real applications
   // can perform other tasks while waiting for the download to complete.
   waiter->WaitOne();
}
// Sample call : DownLoadDataInBackground ("http://www.contoso.com/GameScores.html");
public static void DownloadDataInBackground(string address)
{
    System.Threading.AutoResetEvent waiter = new System.Threading.AutoResetEvent(false);
    WebClient client = new WebClient();
    Uri uri = new Uri(address);
    // Specify that the DownloadDataCallback method gets called
    // when the download completes.
    client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(DownloadDataCallback);
    client.DownloadDataAsync(uri, waiter);
    // Block the main application thread. Real applications
    // can perform other tasks while waiting for the download to complete.
    waiter.WaitOne();
}
'  Sample call : DownLoadDataInBackground ("http:' www.contoso.com/GameScores.html")
Public Shared Sub DownloadDataInBackground(ByVal address As String)
    Dim waiter As System.Threading.AutoResetEvent = New System.Threading.AutoResetEvent(False)
    Dim client As WebClient = New WebClient()
    '  Specify that the DownloadDataCallback method gets called
    '  when the download completes.
    AddHandler client.DownloadDataCompleted, AddressOf DownloadDataCallback
                Dim uri as Uri = New Uri(address)
    client.DownloadDataAsync(uri, waiter)
    '  Block the main application thread. Real applications
    '  can perform other tasks while waiting for the download to complete.
    waiter.WaitOne()
End Sub
下面的代码示例演示此事件的处理程序的实现。
void DownloadDataCallback( Object^ /*sender*/, DownloadDataCompletedEventArgs^ e )
{
   System::Threading::AutoResetEvent^ waiter = dynamic_cast<System::Threading::AutoResetEvent^>(e->UserState);
   try
   {
      // If the request was not canceled and did not throw
      // an exception, display the resource.
      if (  !e->Cancelled && e->Error == nullptr )
      {
         array<Byte>^data = dynamic_cast<array<Byte>^>(e->Result);
         String^ textData = System::Text::Encoding::UTF8->GetString( data );
         Console::WriteLine( textData );
      }
   }
   finally
   {
      // Let the main application thread resume.
      waiter->Set();
   }
}
private static void DownloadDataCallback(Object sender, DownloadDataCompletedEventArgs e)
{
    System.Threading.AutoResetEvent waiter = (System.Threading.AutoResetEvent)e.UserState;
    try
    {
        // If the request was not canceled and did not throw
        // an exception, display the resource.
        if (!e.Cancelled && e.Error == null)
        {
            byte[] data = (byte[])e.Result;
            string textData = System.Text.Encoding.UTF8.GetString(data);
            Console.WriteLine(textData);
        }
    }
    finally
    {
        // Let the main application thread resume.
        waiter.Set();
    }
}
Private Shared Sub DownloadDataCallback(ByVal sender As Object, ByVal e As DownloadDataCompletedEventArgs)
    Dim waiter As System.Threading.AutoResetEvent = CType(e.UserState, System.Threading.AutoResetEvent)
    Try
        '  If the request was not canceled and did not throw
        '  an exception, display the resource.
        If e.Cancelled = False AndAlso e.Error Is Nothing Then
            Dim data() As Byte = CType(e.Result, Byte())
            Dim textData As String = System.Text.Encoding.UTF8.GetString(data)
            Console.WriteLine(textData)
        End If
    Finally
        '  Let the main application thread resume.
        waiter.Set()
    End Try
End Sub
	注解
谨慎
              WebRequest、HttpWebRequest、ServicePoint和 WebClient 已过时,不应将其用于新开发。 请改用 HttpClient。
每次异步数据下载操作完成时都会引发此事件。 异步数据下载是通过调用 DownloadDataAsync 方法启动的。
DownloadDataCompletedEventHandler 是此事件的委托。 DownloadDataCompletedEventArgs 类为事件处理程序提供事件数据。
有关如何处理事件的详细信息,请参阅 处理和引发事件。