0

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

lenden
  • 800
  • 2
  • 14
  • 38

2 Answers2

1

I would use the async/await. Life could be easier...

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri("myurl"));
var response = await request.GetResponseAsync();

using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
{
    string text = streamReader.ReadToEnd();
    // Updating UI is safe. Do it whichever way you want!!!
}

EDIT

You can also use HttpClient

using (HttpClient client = new HttpClient())
{
    string text = await client.GetStringAsync("myurl");    
    //update UI here
}
EZI
  • 15,209
  • 2
  • 27
  • 33
  • It can't find method GetResponseAsync.. What using should I add? Also it says that I can use "await" only in methods with async modifier and smth about returning Task object.. – lenden Aug 12 '14 at 20:35
  • Can you give more details about where to use this code, as I understood, I need method with keyword async? – lenden Aug 12 '14 at 20:55
  • @lenden just add `async` to your method declaration (if it is void). If you are returning something than change the declaration from `X` to `Task` – EZI Aug 12 '14 at 20:56
  • It's not available in Phone 8 only in 8.1. http://matthiasshapiro.com/2012/12/10/window-8-win-phone-code-sharing-httpwebrequest-getresponseasync/ – Martijn van Put Aug 12 '14 at 21:27
  • wow.. and what to do? I found HttpWebRequest.Async.Extensions in NuGet, may it help? – lenden Aug 12 '14 at 21:29
  • So, it told me to update vs to version 3, I see that it's installing 8.1 sdk.. May be it will help to fix problem.. – lenden Aug 12 '14 at 21:49
  • @lenden Maybe you should switch to HttpClient – EZI Aug 13 '14 at 07:02
  • when a function awaits another one (has `await` in its implementation), then the function itself must be declared as async.Otherwise since it blocks waiting for something, anyone who calls it would be blocked too! That is why you have to add `async` to its declaration. The documentation provided by EZI is surprisingly very good. – Vassilis Apr 21 '16 at 02:06
0
public static void DoOnUIThread(some parameters)
{
  // You can use parameters now.  Obviously "some" is not a type, but you would replace this with the type you're using for your parameters.
}

public static void AddEventTypeToList(some parameters)
{
    // Change Action<Type> to use the type of the parameter you want
       Deployment.Current.Dispatcher.BeginInvoke(new Action<some>(DoOnUIThread));
}   
Jon
  • 3,230
  • 1
  • 16
  • 28