如何在主线程中获取异步结果 [英] How to get async result in main thread

查看:34
本文介绍了如何在主线程中获取异步结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Windows Phone 7 应用程序上制作登录页面.当登录错误消息从异步线程上的服务器返回时,我想在登录页面上获取登录错误状态代码.

I'm making a login page on Windows Phone 7 app. I'd like to get login error status code on the login page when login error message return from server on async thread.

所以我的问题是:在下面的代码示例中,请告诉我如何在 Main 方法中获得responseString(string)"?

So my question is : In bellow code sample, please let me know how do you get "responseString(string)" in Main method?

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream.aspx

using System;
using System.Net;
using System.IO;
using System.Text;
using System.Threading;

class HttpWebRequestBeginGetRequest
{
    private static ManualResetEvent allDone = new ManualResetEvent(false);

    public static void Main(string[] args)
    {


        // Create a new HttpWebRequest object.
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.contoso.com/example.aspx");

        request.ContentType = "application/x-www-form-urlencoded";

        // Set the Method property to 'POST' to post data to the URI.
        request.Method = "POST";

        // start the asynchronous operation
        request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);

        // Keep the main thread from continuing while the asynchronous
        // operation completes. A real world application
        // could do something useful such as updating its user interface. 
        allDone.WaitOne();

        /* I'd like to get "responseString" here. */
    }

    private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

        // End the operation
        Stream postStream = request.EndGetRequestStream(asynchronousResult);

        Console.WriteLine("Please enter the input data to be posted:");
        string postData = Console.ReadLine();

        // Convert the string into a byte array.
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        // Write to the request stream.
        postStream.Write(byteArray, 0, postData.Length);
        postStream.Close();

        // Start the asynchronous operation to get the response
        request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
    }

    private static void GetResponseCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

        // End the operation
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
        Stream streamResponse = response.GetResponseStream();
        StreamReader streamRead = new StreamReader(streamResponse);
        string responseString = streamRead.ReadToEnd(); /* I'd like to get this responseString in Main method. */
        Console.WriteLine(responseString);
        // Close the stream object
        streamResponse.Close();
        streamRead.Close();

        // Release the HttpWebResponse
        response.Close();
        allDone.Set();
    }
}

推荐答案

您可以将 responseString 定义为类级变量,而不是在 GetResponseCallback 方法中定义它.这样,就可以从类中的任何地方访问它,而不仅仅是方法范围.

You could just define responseString as a class-level variable instead of defining it within the GetResponseCallback method. That way, it can be accessed from anywhere in the class, rather than just the method scope.

要从后台线程导航到另一个页面,您可以使用 Dispatcher.

To navigate to another page from a background thread, you can use a Dispatcher.

    //Method to move to next page. Can be called from GetResponseCallBack
    private void NavigateToNextPage()
    {
        Dispatcher.BeginInvoke(() =>
        {
           NavigationService.Navigate(new Uri("Page2.xaml", UriKind.Relative"));
        });
    }

这篇关于如何在主线程中获取异步结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆