5

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?

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

In August 2014, Microsoft released a command line tool and a Visual Studio extension, the .NET Portability Analyzer, that analyzes your project and shows you which platforms it supports. The analyzer shows you which lines of code in your project use APIS that aren't supported by various platforms. The announcement is at blogs.msdn.com/b/dotnet/archive/2014/08/06/…. This seems much more convenient than looking it up in a spreadsheet.
that spreadsheet is now shown online (via Excel Online) and shows up quite fast, so no need to download locally
0

There is a (very) partial implementation available from Microsoft Research (used at automatic graph layout library which is opensource under MIT license now) at:

https://github.com/Microsoft/automatic-graph-layout/blob/master/GraphLayout/MsaglSilverlight/Compatibility.cs

Comments

0

You could manually build a list of tasks and then simply wait for them all:

int whoCares;
var list = new List<int> {1, 2, 3, 4};
List<Task> myTasks = new List<Task>();

foreach (var item in list) // Artificially implement Parallel.For because this is a PCL
{
    myTasks.Add(Task.Run(() =>
    {
        // Task logic goes here
        whoCares += item
    }));
}

await Task.WhenAll(myTasks);

Based on this post

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.