微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/csharp/91563.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐