windows-phone-7 – 如何在主线程中获得异步结果
发布时间:2020-12-14 02:51:56 所属栏目:Windows 来源:网络整理
导读:我正在 Windows Phone 7应用程序上创建一个登录页面.当在异步线程上从服务器返回登录错误消息时,我想在登录页面上获取登录错误状态代码. 所以我的问题是: 在下面的代码示例中,请告诉我如何在Main方法中获得“responseString(string)”? http://msdn.micros
我正在
Windows Phone 7应用程序上创建一个登录页面.当在异步线程上从服务器返回登录错误消息时,我想在登录页面上获取登录错误状态代码.
所以我的问题是: 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,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方法中定义它.这样,它可以从类中的任何位置访问,而不仅仅是方法范围.
要从后台线程导航到另一个页面,您可以使用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")); }); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- [主板] [教程]在Windows 2012 R2上安装Intel I217-V/I218-V
- 升级到Windows 10后,批处理文件中的powershell.exe调用无法
- windows – 批处理文件中的“%1”和“%2”是什么?
- Windows 2008 R2 CA和自动注册:如何摆脱> 100,000个颁发的
- Windows7安装 docker-compose的过程
- Windows – RNDIS小工具无法启动
- 良好的开源错误跟踪/问题跟踪sofware for Windows
- windows – cygwin console2:运行cygwin bash与启动目录
- iis-7 – 必须运行IISRESET两次才能启动所有与IIS相关的服务
- 如何仅使用windows命令shell查找文件大小?