Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using System.Threading.Tasks.Parallel with .NET portable subset

I am trying to use System.Threading.Tasks.Parallel.ForEach in a portable class library project using profile 158 (targeting .NET for Windows Store Apps, .NET Framework 4.5, Silverlight 5, and Windows Phone 8.) I'm running Visual Studio Professional 2012 Update 3.

When I write code like this:

var list = new List<int> {1, 2, 3, 4};
int whoCares;
System.Threading.Tasks.Parallel.ForEach(list, (item) => whoCares += item);

the compiler says that "The type or namespace name 'Parallel' does not exist in the namespace 'System.Threading.Tasks'" (I realize this example code is not actually thread-safe.)

I've confirmed on MSDN that Parallel.ForEach is supported for portable class libraries. If I use the Object Browser to browse my reference to ".NET Portable Subset", I see System.Threading.Tasks.Parallel, member of component ".NET Portable Subset", and it does indeed have definitions for System.Threading.Tasks.Parallel.ForEach.

What do I have to do to get access to Parallel.ForEach in my PCL project?

like image 723
Tim Crews Avatar asked Oct 20 '13 19:10

Tim Crews


1 Answers

The MSDN introduction to Portable Class Libraries, in the section titled Supported Types and Members indicates that you can simply refer to the Version Information section of a given MSDN help topic to determine if a member is supported for Portable Class Libraries. Alternatively, when looking at the members table for a class, a certain icon will appear next to members that are supported for PCL.

So, for example, in the documentation for the System.Threading.Tasks Namespace, the table entry for Parallel does have the icon indicating it is supported for PCL. The reference documentation for Parallel.ForEach has a Version Information section that indicates "Supported in: Portable Class Library".

It turns out that this information is not granular enough. More granular information is published by Microsoft, but as far as I know it is only available in the form of an Excel spreadsheet. It was originally linked from the Microsoft blog article How to Make Portable Class Libraries Work for You. The shortened URL provided by Microsoft is http://sdrv.ms/OVdfNc, which expands to https://skydrive.live.com/view.aspx?resid=8A7FB4A4B32FB2C9!180&app=Excel&authkey=!AHaBmLAhQ49YCI0

This is a 1.1MB spreadsheet. Alas, in this spreadsheet it is shown that Parallel.ForEach is supported for .NET 4.5, but is not supported by SL5 or WP8.

like image 181
Tim Crews Avatar answered Oct 07 '22 17:10

Tim Crews