Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
Mono organizations on
GitHub to continue tracking issues. Bugzilla will remain
available for reference in read-only mode. We will continue to work
on open Bugzilla bugs, copy them to the new locations
as needed for follow-up, and add the new items under Related
Our sincere thanks to everyone who has contributed on this bug
tracker over the years. Thanks also for your understanding as we
make these adjustments and improvements for the future.
Please create a new report for Bug 2916 on
Developer Community or GitHub if you have new
information to add and do not yet see a matching new report.
If the latest results still closely match this report, you can use the
In special cases on GitHub you might also want the comments:
GitHub Markdown with public comments
When invoking WebClient.DownloadStringAsync() from the main (UI) thread, the WebClient.DownloadStringCompleted event is also raised on the main (UI) thread; calling DownloadStringAsync() from a non-UI thread will raise the DownloadStringCompleted event on a non-UI thread.
For compatibility/consistency, Mono for Android should do the same. Perhaps this requires some TPL integration?
It does seem that the rules now for WebClient are now effectively random for anyone trying to reuse code across platforms.
In particular, the WebClient behavior would not work the same way on the desktops.
And the rationale on the blog is a bit flawed. If the idea was to not clog the UI thread, by delivering the data on the UI thread in response to a request, they just clogged the UI thread.
A bit of investigation on a standard .Net 4.0 WPF application produces the following results.
I created a method called DownloadData() which calls WebClient.DownloadStringAsync after setting up a the DownloadStringCompleted event handler.
Calling DownloadStringAsync on the main UI thread raised the callback on the same ui thread.
Calling it from a System.Threading.Thread The DownloadData is not called on the main thread as you would expect however the event handler is raised on a different thread entirely, probably the one that DownloadStringAsync created. The same is true if you use BackgroundWorker or a Task.
So it appears under .net 4.0 on the desktop the event is only marshaled back onto the UI thread if it was original called from the ui thread in the first place, otherwise the event is raised on the same thread that the Async method created itself.
Created attachment 2905 [details]
@Martin, any update for this one?