1

I need to implement asynchronous downloading by using WebClient.DownloadDataAsync Method

I've defined event handler as following:

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

But I need to pass to this event handler an additional parameter together with downloaded result , value with which I created URI. For example:

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

I need to get myUniqueUserIdentifier on callback in DownloadCompleted sub.

I'm registering my event handler by using the following code:

AddHandler webClient.DownloadDataCompleted, AddressOf DownloadCompleted

I've only one guess, to extend WebClient object and to override DownloadDataCompleted object. But before I'm going to perform this I'd like to check is there more simpler solution?

Thank you.

5
  • please fix the title to something meaningful and related to the question and which will help future visitors...and we dont need tags in the title Commented Jun 3, 2015 at 14:49
  • Sorry, I've fixed it already... Commented Jun 3, 2015 at 14:50
  • @Anatoly: Can you attache it to sender? Commented Jun 3, 2015 at 14:51
  • @huMptyduMpty, I don't know. I came from Java world and only occasionally touching vb.net when need to fix some legacy code. I'm looking for any direction where should I move or simple example how to do it. Thank you. Commented Jun 3, 2015 at 14:55
  • 1
    @Anatoly : Please look at Add Custom Event to a Class in VB.NET Commented Jun 3, 2015 at 15:00

2 Answers 2

5

You can use the WebClient.DownloadDataAsync overload which takes an additional object, and then recieve it again via the AsyncCompletedEventArgs.UserState property.

Here's an example:

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 
Sign up to request clarification or add additional context in comments.

Comments

1

You can use a function to pass the parameter instead of AddressOf

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.