Windows Phone 7JSON HTTPWebrequest Synchronous

本文关键字:Synchronous HTTPWebrequest 7JSON Phone Windows | 更新日期: 2023-09-27 17:56:25

我们有一个线程,需要在Windows Phone 7中同步。我们需要一个接一个地发送HTTPWebrequest,直到线程完成,以便下一个线程可以启动。由于WP7是异步的,我们需要一个同步的解决方案。输出采用 JSON 的形式。

Windows Phone 7JSON HTTPWebrequest Synchronous

你不能(也不应该!!)进行同步。而是使用队列机制或具有新 Async/await 任务模式的代码,这允许您或多或少地编写代码,就好像它是同步的一样

我尝试使用ASYNC/Await。在调用响应之前,它变为异步。这是供您参考的代码。

            HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
            Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);
            // creating JSON object
            JObject json =
            new JObject(new JProperty(VMConstants.JSON_CONSTANT_LOGINCMD, new JObject(
            new JProperty("employeeId", constant.EMPLID)
            )));
            JsonSerializer serializer = new JsonSerializer();
            serializer.NullValueHandling = NullValueHandling.Ignore;
            using (StreamWriter sw = new StreamWriter(postStream))
            using (JsonWriter writer = new JsonTextWriter(sw))
            {
                json.WriteTo(writer, null);
            }
            webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
                    // Start the web reponse
            postStream.Close();