I'm sending HttpWebRequest from Windows Phone application:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri("myurl"));
request.BeginGetResponse(MyProcessor, request);
Here's callback:
public void MyProcessor(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
{
string text = streamReader.ReadToEnd();
// passing some arguments to special class, that generates ui elements
}
}
Here's method of generator class
public static void AddEventTypeToList(some parameters)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
//xaml generating
});
}
SO. I need to use Dispatcher.BeginInvoke, because I get "Invalid cross-thread access" otherwise.
BUT. I also need to pass references to some parameters... And I get error "You cannot user parameter with keyword ref or out in anonym method, lambda-expression or query expression.
What to do? I generally need to use HttpWebRequest(because I need to use status codes and it blocks WebClient for me) and pass references to some ui elements to special uigenerator class in the callback
I tried Mangist answer, but have some errors as I think:
public static void AddEventTypeToList(EventType ev, ListBox mainListBox, ref List<Grid> inds)
{
Deployment.Current.Dispatcher.BeginInvoke(new Action<EventType, ListBox, List<Grid>>(DoOnUIThread));
}
Generating:
public static void DoOnUIThread(EventType ev, ListBox mainListBox, List<Grid> inds)
{
Grid gr = new Grid();
//....
}
It doesn't reach breakpoints in this method at all