简体   繁体   中英

Windows Phone 7JSON HTTPWebrequest Synchronous

We have a thread where it needs to be synchronous in Windows Phone 7. We need to send HTTPWebrequest one by one until the thread is completed, so that the next thread can start . As WP7 is asynchronous , we need a solution which is made synchronous. the output is in the form of JSON.

You can't (and shouldn't!!!) make this synchronous. instead use a queuing mechanism, or code with the new async/await task pattern, which allow you to more or less write code as if it was synchronous

I tried with ASYNC/Await. Before the Response gets called , it becomes asynchronous. Here is the code for your reference.

            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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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