VichitraVij
VichitraVij

Reputation: 359

How to synchronize HttpRequest or WebClient in Wp7?

Now I know i can only dowload a string asynchronously in Windows Phone Seven, but in my app i want to know which request has completed. Here is the scenario: I make a certain download request using WebClient() i use the following code for download completed

WebClient stringGrab = new WebClient(); stringGrab.DownloadStringCompleted += ClientDownloadStringCompleted; stringGrab.DownloadStringAsync(new Uri(<some http string>, UriKind.Absolute));

i give the user the option of giving another download request if this request takes long for the user's liking. my problem is when/if the two requests return, i have no method/way of knowing which is which i.e. which was the former request and which was second! is there a method of knowing/sychronizing the requests?

I can't change the requests to return to different DownloadStringCompleted methods!

Thanks in Advance!

Upvotes: 0

Views: 1602

Answers (3)

Matt Lacey
Matt Lacey

Reputation: 65564

If the requests are essentially the same, rather than keep track of which request is being returned. Why not just keep track of if one has previously been returned? Or, how long since the last one returned.

If you're only interested in getting this data once, but are trying to allow the user to reissue the request if it takes a long time, you can just ignore all but the first successfully returned result. This way it doesn't matter how many times the user makes additional requests and you don't need to track anything unique to each request.

Similarly, if the user can request/update data from the remote service at any point, you could keep track of how long since you last got successfull data back and not bother updating the model/UI if you get another resoponse shortly after that. It'd be preferable to not make requests in this scenario but if you've got to deal with long delays and race conditions in responses you could use this technique and still keep the UI/data up to date within a threshold of a few minutes (or however long you specify).

Upvotes: 0

ehnmark
ehnmark

Reputation: 2736

Why not do something like this:

void DownloadAsync(string url, int sequence)
{
    var stringGrab = new WebClient();
    stringGrab.DownloadStringCompleted += (s, e) => HandleDownloadCompleted(e, sequence);
    stringGrab.DownloadStringAsync(new Uri(url, UriKind.Absolute));
}

void HandleDownloadCompleted(DownloadStringCompletedEventArgs e, int sequence)
{
    // The sequence param tells you which request was completed
}

Upvotes: 3

Den
Den

Reputation: 16826

It is an interesting question because by default WebClient doesn't carry any unique identifiers. However, you are able to get the hash code, that will be unique for each given instance.

So, for example:

WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(new Uri("http://www.microsoft.com", UriKind.Absolute));

WebClient client2 = new WebClient();
client2.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client2.DownloadStringAsync(new Uri("http://www.microsoft.com", UriKind.Absolute));

Each instance will have its own hash code - you can store it before actually invoking the DownloadStringAsync method. Then you will add this:

int FirstHash = client.GetHashCode();
int SecondHash = client2.GetHashCode();

Inside the completion event handler you can have this:

if (sender.GetHashCode() = FirstHash)
{
    // First completed
}
else
{
    // Second completed
}

REMEMBER: A new hash code is given for every re-instantiation.

Upvotes: 1

Related Questions