我该如何等待响应并对其进行操作?

时间:2011-06-29 11:51:27

标签: windows-phone-7 webrequest manualresetevent waitone

我有一个新问题。我想用响应做一些操作,但是我得到一个NullReferenceException,因为它还没有到达......这是我的代码:

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; });
        }
    }
}

但是当我使用ManualResetEvent类时,我的应用程序只是悬挂,因为 if(dataReady.WaitOne())行。以下是ManualResetEvent类的完整代码:

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();
    }
}

所以,我的问题是:如何等待响应并对其进行操作? (我尝试使用Application.DoEvent方法,但在WP7中不存在...)

3 个答案:

答案 0 :(得分:1)

你不能把那段代码

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

on request_CallBack函数?

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
    }

答案 1 :(得分:0)

目前您正在使用WebRequest的异步方法之一。如果要等待响应,可以使用等效的同步方法GetResponse

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

答案 2 :(得分:-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
}

}