Note that there are some explanatory texts on larger screens.

plurals
  1. POWebClient DownloadStringAsync blocked - never finished
    primarykey
    data
    text
    <p>I have specific problem with WebClient in my Windows Phone app (using MVVM)</p> <pre><code>private string _lastCurrencyRatesJson; private bool _lastCurrencyRatesJsonLoaded = false; private void GetLastCoursesFromApiAsync() { var uri = new Uri(string.Format(OperationGetLastCourses, AppSettings.ApiEndpoint, AppSettings.ApiKey)); var client = new WebClient { Encoding = Encoding.UTF8 }; client.DownloadStringCompleted += client_DownloadStringCompleted; client.DownloadStringAsync(uri); } void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { _lastCurrencyRatesJson = e.Result; _lastCurrencyRatesJsonLoaded = true; } public List&lt;CurrencyRate&gt; GetLastCourses() { var worker = new Thread(GetLastCoursesFromApiAsync); worker.Start(); while (!_lastCurrencyRatesJsonLoaded) { } ..... </code></pre> <p>The problem is that client_DownloadStringCompleted is never fired BUT when I change GetLastCourses this way:</p> <pre><code>public List&lt;CurrencyRate&gt; GetLastCourses() { var worker = new Thread(GetLastCoursesFromApiAsync); worker.Start(); // whetever here, but any while... </code></pre> <p>client_DownloadStringCompleted is fired and data are obtained. It means, connectivity is ok.</p> <p>I had very similar problems with DownloadStringTaskAsyn. Example:</p> <pre><code>private async Task&lt;string&gt; GetCoursesForDayFromApiAsJson(DateTime date) { var uri = new Uri(string.Format(OperationGetCoursesForDay, AppSettings.ApiEndpoint, AppSettings.ApiKey, date.ToString(DateFormat))); var client = new WebClient { Encoding = Encoding.UTF8 }; return await client.DownloadStringTaskAsync(uri); } </code></pre> <p>Again, at the line with await is application waiting for the data but the DownloadStringTaskAsync is never finished and my UI is still loading.</p> <p>Any ideas what could be wrong?</p> <hr> <p><strong>SITUATION ONE DAY AGO</strong> So, it looks that WP application is working just with one thread. It means, current thread have to be "finished" and then is DownloadStringTaskAsync finished and the code under the await executed. When I want to work with Task.Result I can not. Never.</p> <p>When I create another Thread and I am trying to wait for thread completetion (using Join()), created Thread is never finsihed and the code after Join() is never executed.</p> <p>There is any example on the Internet and I absolutely don't know, why exists some DownloadStringTaskAsync when it is not applicable.</p>
    singulars
    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.
 

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