c# – WP7应用程序永远不会退出BeginGetResponse并进入回调函数

前端之家收集整理的这篇文章主要介绍了c# – WP7应用程序永远不会退出BeginGetResponse并进入回调函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下代码
private 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();
            string postData = "my data";

            // 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
                IAsyncResult result =
                      (IAsyncResult)request.BeginGetResponse(new AsyncCallback(GetResponseCallback),request);

        }

        private 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();
            Console.WriteLine(responseString);
            // Close the stream object
            streamResponse.Close();
            streamRead.Close();

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

            Dispatcher.BeginInvoke((Action)(() => Debug.WriteLine("George")));
        }

但是,当我的代码命中BeginGetResponse时,它永远不会退出(我在GetResponseCallback函数中没有遇到断点).我尝试添加BeginInvoke调用,但我仍然没有输入此方法.此代码适用于Windows控制台应用程序 – 它位于Windows Phone 7上,它不具备此功能

谁能看到我做错了什么?

谢谢.

解决方法

如果您已在UI线程上创建了HttpWebRequest,那么请确保您不阻止UI线程,否则您可能会死锁.

来自您链接的桌面.NET的示例未针对当前的电话网络堆栈进行优化.您应该更改代码,以便在后台线程上创建HttpWebRequest.

原文链接:https://www.f2er.com/csharp/91563.html

猜你在找的C#相关文章