2

I'm playing with the most simplest case of usage bcl.async in wp7.

private async void loadButton_Click1(object sender, RoutedEventArgs e)
{
        var client = new WebClient(); 
        string response = await client.DownloadStringTaskAsync(new Uri("myurl"));
        Debug.WriteLine(response);
}

So, i have breakpoints on the first and last lines, running in on device in debug mode. Sometimes it takes about 5 seconds to get the answer (which is weird), but sometimes it takes almost minute. During 1 request from device, i'm able to check request in the browser (getting instant answer, less than second), got some tea, and check 9gag.

I can assume that i have bad networking, but browser's request is lightspeed, so that is not a problem. Also, i tried RestSharp, but faced the same issue. Non-async WebClient calls work fine.

EDIT: FAST SOLUTION: very first thing you want to do is to restart device.

Vitalii Vasylenko
  • 4,776
  • 5
  • 40
  • 64
  • I suggest testing with a known url (such as `http://www.google.com`) - just to be sure that the problem is in your code. After that, I would try using the old-fashion async web request (not using `async` and `await`) - again, just to be sure. – SimpleVar May 01 '13 at 07:34
  • Try to use this suggestion from following link [1] [1]: http://stackoverflow.com/questions/4932541/c-sharp-webclient-acting-slow-the-first-time – Uzzy May 01 '13 at 07:36
  • @Uzzy That sounds good, but wp doesnt have Proxy property. – Vitalii Vasylenko May 01 '13 at 07:42
  • @YoryeNathan Just tried same code with google.com, getting answer in 5-7 seconds. – Vitalii Vasylenko May 01 '13 at 07:44
  • @VitaliiVasylenko Try my other proposal. Also, if you don't really care about the progress of the download, you can manage it's async-ness yourself with simple threading and by that avoiding the usage of the async method. – SimpleVar May 01 '13 at 07:47
  • @YoryeNathan yyeeees i can... but i'm tired from endless combined and nested callbacks, and trying now to move project to asyncs. – Vitalii Vasylenko May 01 '13 at 07:49
  • @VitaliiVasylenko Doing so, even temporarily, can help us find the problem. If we see that old-school-async works just fine for you, you're safe to send a bug report to Microsoft. – SimpleVar May 01 '13 at 07:54
  • @YoryeNathan I'm still hoping that some magic trick would help (like mentioned client.Proxy = null;): i dont think that i'm first one, who faced it. – Vitalii Vasylenko May 01 '13 at 07:59
  • I think HttpClient is more adept at Async work ... – Jammer May 01 '13 at 07:59
  • @Jammer thanks, so i'll make a new clean solution, and test both – Vitalii Vasylenko May 01 '13 at 08:11

1 Answers1

1

As I mentioned in the comment I think it would be worth looking into using HttpClient rather than WebClient. HttpClient is newer and designed with the new WebAPI in mind and I believe designed to work with greater efficiency in general.

There is a good comparison listed here.

Jammer
  • 9,969
  • 11
  • 68
  • 115
  • Thanks for comparison table, but looking like, [HttpClient is not avaivable in wp7](http://stackoverflow.com/questions/10444273/httpclient-and-httpget-support-in-wp7). There's HttpWebRequest, is it the same what you're talking about? – Vitalii Vasylenko May 01 '13 at 08:21
  • Indeed, `HttpClient` makes use of `HttpWebRequest` and `HttpWebResponse` under the hood. I've not spent any time looking at WP7 stuff so I apologise for that oversight on my part. – Jammer May 01 '13 at 08:30
  • Got something about [HttpWebRequest](http://social.msdn.microsoft.com/Forums/en-US/silverlightwinphone/thread/f4208c99-be17-400a-9105-6122ca6263be/) (last reply) – Vitalii Vasylenko May 01 '13 at 08:34
  • Yep, but still some more testings are needed. Also, [here](http://blogs.msdn.com/b/andy_wigley/archive/2013/02/07/async-and-await-for-http-networking-on-windows-phone.aspx) is nice (and up-to-dated) sample about using async HttpWebRequest – Vitalii Vasylenko May 01 '13 at 08:45
  • Nope, sorry to say, but the problem is not solved. I tried this code in the very clean project (wp7 project + bcl.async): http://pastebin.com/hdiQv7tw, and it returned "1: 46748 2: 46714" on the devide. Still something wrong: it took 46 seconds to load content on device, while i was able to open google instantly in my browser. – Vitalii Vasylenko May 01 '13 at 08:55
  • This is odd, I'm not sure this is simply a code related problem to be honest. If others are doing this with success using the same classes I'd say the problem lies elsewhere. Does your phone have good connectivity, is the browser and phone both using the same connectivity (same wifi connection for instance)? – Jammer May 01 '13 at 09:24
  • The only thing i have in mind is that the reason is in phone's powersaving. Testing is still in progress. PS: btw, HttpClient is a little bit faster indeed :) – Vitalii Vasylenko May 01 '13 at 09:35
  • That could well be adversely effecting it. Yes it is faster for sure :) – Jammer May 01 '13 at 09:54
  • Looking like its problem with async :(((( Just run old good callback and async, callback is much faster 0_0 But [it shouldn't be faster](http://stackoverflow.com/questions/7870334/await-async-vs-classic-asynchronous-callbacks) – Vitalii Vasylenko May 01 '13 at 10:13
  • Hmm, that's a lot to take on-board from Theo in that thread. I've run out of suggestions I'm afraid. :( – Jammer May 01 '13 at 10:38
  • As usual in Windows, phone reboot solved all stranged-behavior problems :) – Vitalii Vasylenko May 01 '13 at 12:51
  • Haha! If in doubt ... glad you got it sorted. I wonder what wp7 was coughing about? – Jammer May 01 '13 at 13:06
  • As usual, no one have an idea (maybe, except some techleads in MS). :) But now i'm getting answers from the server fast enough (in several seconds... still slower than browser, but much better than 40). – Vitalii Vasylenko May 01 '13 at 13:10
  • It's difficult comparing a desktop browser to a phone I think. Like comparing apples and oranges really. They are different beasts at the end of the day as long its comparable, which in now appears to be I'd say that's a win. – Jammer May 01 '13 at 13:20
  • Actually, not.. say, phone browser is quite fast, and loads http://stackoverflow.com/ pretty fast (just checked, never visited before). Of course, we can't compare PC and device speeds directly, but i see no 3-seconds delay before starting loading page on device. – Vitalii Vasylenko May 01 '13 at 13:26
  • My point is really there are so many variables involved that comparing them directly isn't straight forward in itself. Lots of factors to take into account I guess. – Jammer May 01 '13 at 13:35
  • Ok. Lets change formulation :) I mean, in device browser, there's no 3-seconds delay before starting to show data. While in my device app, i see that delay. – Vitalii Vasylenko May 01 '13 at 13:44
  • 1
    Sounds legit, but i have no code in that project :) Just a clean WP 7 project + bcl.async. I consider only hardware issues: unformatted drive ot some crap like that. I also tried release mode, so i think, its device hardware problems. – Vitalii Vasylenko May 01 '13 at 16:19
  • That's annoying to say the least. – Jammer May 01 '13 at 16:31