0

I have one new problem. I want to do some operations with the response, but I get a NullReferenceException, because it isn't arrived yet... Here is my code:

public partial class MainPage : PhoneApplicationPage
{
    public static string res = null;

    // Constructor
    public MainPage()
    {
        InitializeComponent();
        string Url = "http://twitter.com";
        WebRequest req = WebRequest.Create(Url);
        req.BeginGetResponse(new AsyncCallback(request_CallBack), req);
        int i = MainPage.res.Length; // NullReferenceException
    }

    void request_CallBack(IAsyncResult result)
    {
        WebRequest webRequest = result.AsyncState as WebRequest;
        WebResponse response = (WebResponse)webRequest.EndGetResponse(result);
        Stream baseStream = response.GetResponseStream();
        using (StreamReader reader = new StreamReader(baseStream))
        {
            res = reader.ReadToEnd();
            Dispatcher.BeginInvoke(() => { MessageBox.Show("The response is arrived."); });
            Dispatcher.BeginInvoke(() => { tbResponse.Text = res; });
        }
    }
}

But when I use the ManualResetEvent class, my app is just hanging, because of the if(dataReady.WaitOne()) line. Here is the complete code with the ManualResetEvent class:

public partial class MainPage : PhoneApplicationPage
{
    public static string res = null;
    ManualResetEvent dataReady;

    // Constructor
    public MainPage()
    {
        InitializeComponent();
        string Url = "http://twitter.com";
        dataReady = new ManualResetEvent(false);
        WebRequest req = WebRequest.Create(Url);
        req.BeginGetResponse(new AsyncCallback(request_CallBack), req);
        if (dataReady.WaitOne())
        {
            int i = MainPage.res.Length;
        }
    }

    void request_CallBack(IAsyncResult result)
    {
        WebRequest webRequest = result.AsyncState as WebRequest;
        WebResponse response = (WebResponse)webRequest.EndGetResponse(result);
        Stream baseStream = response.GetResponseStream();
        using (StreamReader reader = new StreamReader(baseStream))
        {
            res = reader.ReadToEnd();
            Dispatcher.BeginInvoke(() => { MessageBox.Show("The response is arrived."); });
            Dispatcher.BeginInvoke(() => { tbResponse.Text = res; });
        }
        dataReady.Set();
    }
}

So, there is my question: How can I wait the response and do operations with it? (I tried to use the Application.DoEvent method, but it isn't exist in WP7...)

laszlokiss88
  • 4,001
  • 3
  • 20
  • 26

3 Answers3

1

cannot you put that code

int i = MainPage.res.Length; // NullReferenceException

on request_CallBack function ?

like

void request_CallBack(IAsyncResult result)
    {
        WebRequest webRequest = result.AsyncState as WebRequest;
        WebResponse response = (WebResponse)webRequest.EndGetResponse(result);
        Stream baseStream = response.GetResponseStream();
        using (StreamReader reader = new StreamReader(baseStream))
        {
            res = reader.ReadToEnd();
            Dispatcher.BeginInvoke(() => { MessageBox.Show("The response is arrived."); });
            Dispatcher.BeginInvoke(() => { tbResponse.Text = res; });
        }
    int i = res.Length; //here
    }
Mujah Maskey
  • 8,654
  • 8
  • 40
  • 61
  • Yes, it's true, but my code is just a simplified example... After the `req.BeginGetResponse(new AsyncCallback(request_CallBack), req);` line there are a lot of functions, which will run... So I have to wait for the response, just I don't know how... – laszlokiss88 Jun 29 '11 at 13:50
0

Currenlty you are using one of the asynchronous methods of WebRequest. If you want to wait for a response, you can simply use the equivalent synchronous method GetResponse.

http://msdn.microsoft.com/en-us/library/system.net.webrequest.getresponse.aspx

ColinE
  • 68,894
  • 15
  • 164
  • 232
-1
public partial class MainPage : PhoneApplicationPage

{ public static string res = null;

// Constructor
public MainPage()
{
    InitializeComponent();
    string Url = "http://twitter.com";
    WebRequest req = WebRequest.Create(Url);
    req.BeginGetResponse(new AsyncCallback(request_CallBack), req);

}

void request_CallBack(IAsyncResult result)
{
    WebRequest webRequest = result.AsyncState as WebRequest;
    WebResponse response = (WebResponse)webRequest.EndGetResponse(result);
    Stream baseStream = response.GetResponseStream();
    using (StreamReader reader = new StreamReader(baseStream))
    {
        res = reader.ReadToEnd();
        Dispatcher.BeginInvoke(() => { MessageBox.Show("The response is arrived."); });
        Dispatcher.BeginInvoke(() => { tbResponse.Text = res; });
    }
    int i = MainPage.res.Length; //No  NullReferenceException
}

}

Ishti
  • 325
  • 1
  • 7
  • 19
  • Yes, it's true, but my code is just a simplified example... After the req.BeginGetResponse(new AsyncCallback(request_CallBack), req); line there are a lot of functions, which will run and they need the response... I can't put all of those functions in the callback. – laszlokiss88 Jul 01 '11 at 13:48