Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Servy is correct in saying you shouldn't block the UI with synchronous calls. </p> <p>With that being said, I had a similar requirement in a WP7 app. I used "Async CTP" to add the ability to write synchronous like functions that wait until it's done before making the next call. I had API calls that required data from earlier calls to function correctly. </p> <p>I believe this was included in .NET 4.5 and works on WP8. </p> <p><a href="http://msdn.microsoft.com/en-ca/library/vstudio/hh191443.aspx" rel="nofollow">http://msdn.microsoft.com/en-ca/library/vstudio/hh191443.aspx</a></p> <p>Here is what I wrote to pull data for my app (Notice the "<strong>async</strong>" and "<strong>await</strong>" keywords):</p> <pre><code>public async Task&lt;bool&gt; GetDefaultData() { try { _cancellation = new CancellationTokenSource(); UpdateProgress(DateTime.Now + ": Download Started.\n"); List&lt;string&gt; data = await App._apiConnection.DoWorkAsync(_cancellation.Token, ApiInfo.GetBaseDataUriList()); App._apiConnection.Done(data); UpdateProgress(DateTime.Now + ": Countries: " + App._context.Countries.Count() + "\n"); UpdateProgress(DateTime.Now + ": Regions: " + App._context.Regions.Count() + "\n"); UpdateProgress(DateTime.Now + ": Income Levels: " + App._context.IncomeLevels.Count() + "\n"); UpdateProgress(DateTime.Now + ": Indicators: " + App._context.Indicators.Count() + "\n"); data = await App._apiConnection.DoWorkAsync(_cancellation.Token, ApiInfo.GetCountryUriList("CA")); App._apiConnection.Done(data); UpdateProgress(DateTime.Now + ": CA Population: " + App._context.PopulationDatas.Count(c =&gt; c.Country.Iso2Code == "CA") + "\n"); data = await App._apiConnection.DoWorkAsync(_cancellation.Token, ApiInfo.GetCountryUriList("US")); App._apiConnection.Done(data); UpdateProgress(DateTime.Now + ": US Population: " + App._context.PopulationDatas.Count(c =&gt; c.Country.Iso2Code == "US") + "\n"); data = await App._apiConnection.DoWorkAsync(_cancellation.Token, ApiInfo.GetCountryUriList("CN")); App._apiConnection.Done(data); UpdateProgress(DateTime.Now + ": CN Population: " + App._context.PopulationDatas.Count(c =&gt; c.Country.Iso2Code == "CN") + "\n"); return true; } catch (OperationCanceledException) { MessageBox.Show("Operation Cancelled"); return false; } catch (Exception ex) { MessageBox.Show("getDefaultData Exception: " + ex.Message); return false; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload