Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight: How to pass data from the request to the response using Webclient Asynchronous mode?

Tags:

c#

silverlight

How to access VIP in the proxy_OpenReadCompleted method?

void method1() 
{ 
    String VIP = "test";
    WebClient proxy = new WebClient();
    proxy.OpenReadCompleted += new OpenReadCompletedEventHandler(proxy_OpenReadCompleted);
    String urlStr = "someurl/lookup?q=" + keyEntityName + "&fme=1&edo=1&edi=1";
}

void proxy_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{ 

}
like image 727
prajor Avatar asked Oct 14 '22 06:10

prajor


1 Answers

There are two approaches to this. First is to pass the string as the second parameter in the OpenReadAsync call, this parameter becomes the value of the UserState property of the event args.

void method1() 
{ 
    String VIP = "test";
    WebClient proxy = new WebClient();
    proxy.OpenReadCompleted += proxy_OpenReadCompleted;
    String urlStr = "someurl/lookup?q=" + keyEntityName + "&fme=1&edo=1&edi=1";
    proxy.OpenReadAsync(new Uri(urlStr, UriKind.Relative), VIP);
}    

void proxy_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{ 
   String VIP = (string)e.UserState;
   // Do stuff that uses VIP.
}

Another approach is to access the variable directly using a closure:-

void method1() 
{ 
    String VIP = "test";
    WebClient proxy = new WebClient();
    proxy.OpenReadCompleted += (s, args) =>
    {
         // Do stuff that uses VIP.
    }
    String urlStr = "someurl/lookup?q=" + keyEntityName + "&fme=1&edo=1&edi=1";
    proxy.OpenReadAsync(new Uri(urlStr, UriKind.Relative), VIP);
}    
like image 198
AnthonyWJones Avatar answered Nov 15 '22 05:11

AnthonyWJones