Domanda

I am making a REST call, the server side response is in the form of an XML. I am making this call asynchronously. I've test it as a Console application and it works as it should. However, when I test it on the XBOX , the asynchronous request is never completed. My processVideo method parses the XML and places the items into a List. I needed to reference this List from another class so I added (result.IsCompleted == false) to ensure that asynchronous call is completed before I reference and utilize the List. It seems that the asynchronous request is never completed and locks UI, any ideas?

 public void initilaizeRest()
    {
        WebRequest request = HttpWebRequest.Create(URI);
        request.Method = GET;
        // RequestState is a custom class to pass info to the callback
        RequestState state = new RequestState(request, URI);
        IAsyncResult result = request.BeginGetResponse(new AsyncCallback(getVideoList), state);

        Logger.Log("Querystate :"+QUERYSTATE+" URI:"+URI);

        /// Wait for aynchronous response to be completed
        while (result.IsCompleted == false)
        {
            Logger.Log("Sleeping");
            Thread.Sleep(100);
        }

    }

  public void getVideoList(IAsyncResult result)
    {
        RequestState state = (RequestState)result.AsyncState;
        WebRequest request = (WebRequest)state.Request;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);


        //Process response
        switch (QUERYSTATE)
        {
            case (int)Querystate.SERIESQUERY:
                Logger.Log("Processing series state");
                processSeries(response);
                break;
            case (int)Querystate.VIDEOQUERY:
                Logger.Log("Processing video state");
                processVideo(response);
                break;
        }

    }

public void processVideo(HttpWebResponse response)
{
      //parses XML into an object  and places items in a LIST
}
È stato utile?

Soluzione

The while loop is your problem. You shouldn't wait like this for the async call to complete. You should do whatever you are wanting to do in the async callback that you send to the Begin method. The reason is that UI sets up a synchronization context which is used for async callbacks. The way this works is that the callbacks are marshalled onto the UI thread so that the UI context is maintained. Because your while loop is blocking your UI thread, the callback never occurs resulting in async call not completing.

Hope this helps.

Altri suggerimenti

getVideoList will be called when the asynchronous operation completes. If you need your UI to not allow certain actions until this operation completes, you'll have to disable them before the call to BeginGetResponse. Remove any loop waiting for IsCompleted on the main UI thread (this will block the UI and make it look "hung"). In getVideoList, re-enable the parts of the UI that should be re-enabled when the call to BeginGetResponse completes.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top