使用DownloadDataCompleted方法

时间:2015-06-03 14:48:41

标签: vb.net webclient

我需要使用WebClient.DownloadDataAsync Method

实现异步下载

我已将事件处理程序定义如下:

 Private Sub DownloadCompleted(sender As Object, e As DownloadDataCompletedEventArgs)
        Dim bytes() As Byte = e.Result
        'do somthing with result
    End Sub

但是我需要将下载的结果与我创建URI的值一起传递给此事件处理程序一个附加参数。例如:

Dim pictureURI = "http://images.server.sample.com/" + myUniqueUserIdentifier

我需要在myUniqueUserIdentifier sub。

中回调DownloadCompleted

我正在使用以下代码注册我的事件处理程序:

AddHandler webClient.DownloadDataCompleted, AddressOf DownloadCompleted

我只有一个猜测,扩展WebClient对象并覆盖DownloadDataCompleted对象。但是在我要执行此操作之前,我想检查是否有更简单的解决方案?

谢谢。

2 个答案:

答案 0 :(得分:4)

您可以使用带有其他对象的the Eclipse documentation for Services重载,然后通过WebClient.DownloadDataAsync属性再次接收它。

以下是一个例子:

Sub Main
    Dim url = "http://google.com"
    Dim client = new WebClient()
    AddHandler client.DownloadDataCompleted, AddressOf DownloadDataCompleted
    client.DownloadDataAsync(new Uri(url), url) ' <- pass url as additional information...'
End Sub

Sub DownloadDataCompleted(sender as object, e as DownloadDataCompletedEventArgs)
    Dim raw as byte() = e.Result
    ' ... and recieve it in the event handler via the UserState property'
    Console.WriteLine(raw.Length & " bytes received from " & e.UserState.ToString())
End Sub

答案 1 :(得分:0)

您可以使用函数来传递参数而不是AddressOf

AddHandler webClient.DownloadDataCompleted, Function(sender, e) DownloadCompleted(param)