DownloadDataCompletedEventHandler 委托    
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示将用来处理 DownloadDataCompleted 的 WebClient 事件的方法。
public delegate void DownloadDataCompletedEventHandler(System::Object ^ sender, DownloadDataCompletedEventArgs ^ e);public delegate void DownloadDataCompletedEventHandler(object sender, DownloadDataCompletedEventArgs e);type DownloadDataCompletedEventHandler = delegate of obj * DownloadDataCompletedEventArgs -> unitPublic Delegate Sub DownloadDataCompletedEventHandler(sender As Object, e As DownloadDataCompletedEventArgs)参数
- sender
- Object
事件源。
示例
下面的代码示例演示如何下载用户指定的资源。
// 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
下载完成后,将调用以下方法。
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
注解
创建 DownloadDataCompletedEventHandler 委托时,需要标识将处理该事件的方法。 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。 有关事件处理程序委托的详细信息,请参阅 处理和引发事件。
扩展方法
| GetMethodInfo(Delegate) | 获取指示指定委托表示的方法的对象。 |