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

发布于 2024-11-17 14:15:38 字数 2385 浏览 3 评论 0原文

我有一个新问题。我想对响应进行一些操作,但我得到一个 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中不存在......)

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...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谁的年少不轻狂 2024-11-24 14:15:38

您不能将该代码放在

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

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
    }

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
    }
一身仙ぐ女味 2024-11-24 14:15:38

当前您正在使用 WebRequest 的异步方法之一。如果您想等待响应,只需使用等效的同步方法 GetResponse 即可。

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

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

千年*琉璃梦 2024-11-24 14:15:38
public partial class MainPage : PhoneApplicationPage

{
公共静态字符串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
}

}

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
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文