简体   繁体   English

阻止Windows Phone中的异步操作

[英]Blocking asynchronous operation in Windows phone

I am trying send the http post from the windows phone to the server and I am getting some of problem via sending the post data.I put the break point in button_click_1 function and I found that it would not start the asynronous operation . 我正在尝试将Windows帖子从Windows Phone发送到服务器,并且通过发送帖子数据而遇到一些问题。我在button_click_1函数中放置了断点,发现它不会启动异步操作 Beside that, it also block the current thread and I know that this situation is cause by the allDone.waitOne() . 除此之外,它还会阻塞当前线程,我知道这种情况是由allDone.waitOne()引起的。

Why the asynronous operation will not function and how to solve it? 为什么异步操作将不起作用,如何解决?

Thank you for any help. 感谢您的任何帮助。

private void Button_Click_1(object sender, RoutedEventArgs e)
            {
            // Create a new HttpWebRequest object.
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

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

            request.Method = "POST";

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

            allDone.WaitOne();

        }

Asynronous operation : 异步运行

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


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

            string postData = "xxxxxxxxxxx";    
            // 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 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();
            tbtesting.Text = responseString.ToString();

            streamResponse.Close();
            streamRead.Close();

            response.Close();
            allDone.Set();
        }

You're not the first to hit this (see Is it possible to make synchronous network call on ui thread in wpf (windows phone) ). 您不是第一个遇到这种情况的人(请参阅在wpf(Windows Phone)中是否可以在ui线程上进行同步网络调用 )。 If you do this then you deadlock the UI thread on Windows Phone. 如果执行此操作,则会死锁Windows Phone上的UI线程。

The closest you're going to get is to use the async/await on your network calls. 您将获得的最接近的结果是在网络呼叫中使用异步/等待。 There are extension methods you can use to do this as part of the Microsoft.Bcl.Async package on NuGet. 作为NuGet上Microsoft.Bcl.Async程序包的一部分,可以使用一些扩展方法来执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM