在 class 库中使用 Parallel.ForEach
Using Parallel.ForEach in a class library
单纯Parallel.ForEach
不想进图书馆Class
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace UtilyTools
{
public class Why
{
public void gluk()
{
var intList= new List<int> { 1, 2, 3};
int notMatter=0;
Parallel.ForEach(intList, (item) => notMatter+= item);
}
}
// [...]
给我旧的:
CS0103 C# The name Parallel
does not exist in the current context
Project.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework>
</PropertyGroup>
</Project>
根据错误的屏幕截图要求:
nb: 使用的不是红色下划线。
Parallel.ForEach(list, (item) => please += item);
应该是:
Parallel.ForEach(intList, (item) => please += item);
您在变量名称上打错了(已更正)。
此外,please += item
不是 thread safe - you should use Interlocked.Add
。
此外,请确保您的 Target Framework
至少是 .NET Framework 4。
然后,使 100% 确保这一行位于文件的顶部:
using System.Threading.Tasks;
如果您使用的是 .NET Standard,请参阅@m.rogalski 的回答。
由于您使用的是 .NetStandard
库而不是 .NetFramework
,因此您必须将 System.Threading.Tasks.Parallel
作为项目的依赖项包含在内。
单纯Parallel.ForEach
不想进图书馆Class
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace UtilyTools
{
public class Why
{
public void gluk()
{
var intList= new List<int> { 1, 2, 3};
int notMatter=0;
Parallel.ForEach(intList, (item) => notMatter+= item);
}
}
// [...]
给我旧的:
CS0103 C# The name
Parallel
does not exist in the current context
Project.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework>
</PropertyGroup>
</Project>
根据错误的屏幕截图要求: nb: 使用的不是红色下划线。
Parallel.ForEach(list, (item) => please += item);
应该是:
Parallel.ForEach(intList, (item) => please += item);
您在变量名称上打错了(已更正)。
此外,please += item
不是 thread safe - you should use Interlocked.Add
。
此外,请确保您的 Target Framework
至少是 .NET Framework 4。
然后,使 100% 确保这一行位于文件的顶部:
using System.Threading.Tasks;
如果您使用的是 .NET Standard,请参阅@m.rogalski 的回答。
由于您使用的是 .NetStandard
库而不是 .NetFramework
,因此您必须将 System.Threading.Tasks.Parallel
作为项目的依赖项包含在内。