Bug 2916 - WebClient raises events on different threads with new .NETs
Summary: WebClient raises events on different threads with new .NETs
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.0
Hardware: PC Mac OS
: Normal normal
Target Milestone: ---
Assignee: Martin Baulig
Depends on:
Reported: 2012-01-16 14:16 UTC by Jonathan Pryor
Modified: 2016-09-01 19:24 UTC (History)
6 users (show)

Tags: XATriaged
Is this bug a regression?: ---
Last known good build:

Sample Application (12.06 KB, application/x-zip-compressed)
2012-11-12 12:31 UTC, dean.ellis

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and 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 Links.

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 original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Jonathan Pryor 2012-01-16 14:16:57 UTC



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?
Comment 1 Miguel de Icaza [MSFT] 2012-01-18 01:58:28 UTC
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.
Comment 2 dean.ellis 2012-11-12 12:30:41 UTC
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.
Comment 3 dean.ellis 2012-11-12 12:31:17 UTC
Created attachment 2905 [details]
Sample Application
Comment 4 Marek Habersack 2016-09-01 19:24:52 UTC
@Martin, any update for this one?