Question

I need to make some web request from my wp7 app, like take version of db from server and compare it with the local db version, then update db if it's needed and few more thing.

I want to do it in starting screen with some kind of animation, the user will need to wait for it, because this data are crucial for app. When everything would be checked the main manu of app will be lunched.

I want to keep this requests in the main thread. One request -> get response -> second request -> get response... Only one way to do it which I find is to use await - task. I'm asking about other methods/ways to do it.

Every method of

HttpWebRequest

or

WebClient

use AsyncCallback so I can say that I'm forced to make that by this way.

How can I make me process to wait for the result of AsyncCallback and then go on with next method?

Also I want to give feedback to user if he need to check connection to internet, if his app is updating right now..... and from the async method this is impossible, because as it's known we can't deal with UI from background tread

here is part of code where I make request for version of db:

  public WelcomePage()
    {
        InitializeComponent();

        CheckingVersionOFDB();
    }

    private void CheckingVersionOFDB()
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("here my site");

        request.BeginGetResponse(asynchronous_method, request);
    }

    private void asynchronous_method(IAsyncResult ar)
    {


        HttpWebRequest request = (HttpWebRequest)ar.AsyncState;

        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar);

        DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(List<versionContent>));

        Stream responseStream = response.GetResponseStream();

       List< versionContent> serverVer = (List<versionContent>)deserializer.ReadObject(responseStream);

       if (version > Convert.ToInt32((serverVer.ElementAt(0)).dbversion))
       {


       }
       else 
       {

       }

    }

Every of your advice will be priceless for me, thx in advance

Was it helpful?

Solution

Every method of [...] WebClient uses AsyncCallback

This is false. Simply use the methods of WebClient that have Task in their name, i.e.:

  • DownloadFileTaskAsync
  • DownloadStringTaskAsync
  • DownloadDataTaskAsync

Then you can await each of those tasks.

In the event that you're working with some other type that really doesn't have versions of its async methods that return a Task, you can use Task.FromAsync to generate a task from that asynchronous method.

If the asynchronous method is not compatible with FromAsync, either because it fires an event, takes a callback, or you simply have some slight differences in how you wish to define the semantics of the task, you can use a TaskCompletionSource to manually generate a Task given any other method of asynchrony. Just create the TCS and set the result/exception/cancellation based on whatever mechanism the other asynchronous method provides.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top