所有这些代码都在最低级别的类库中。
对于下面的代码块,我应该将整个代码体包装在 a 中Task.Run(()=>{});
还是有更好的方法来触发其中的每一个.GroupParts
?async Parallel 会在哪里发挥作用?
public IGroupedParts GroupParts(GroupOption option)
{
IGroupedParts gParts = new GroupedParts();
if (this.Doors.Count > 0) { gParts.Doors = this.Doors.GroupParts(option); }
if (this.Leafs.Count > 0) { gParts.Leafs = this.Leafs.GroupParts(option); }
if (this.Sidelites.Count > 0) { gParts.Sidelites = this.Sidelites.GroupParts(option); }
if (this.Glass.Count > 0) { gParts.Glass = this.Glass.GroupParts(option); }
if (this.GlassStops.Count > 0) { gParts.GlassStops = this.GlassStops.GroupParts(option); }
if (this.Horizontals.Count > 0) { gParts.Horizontals =this.Horizontals.GroupParts(option); }
if (this.Verticals.Count > 0) { gParts.Verticals = this.Verticals.GroupParts(option); }
if (this.Sills.Count > 0) { gParts.Sills = this.Sills.GroupParts(option); }
if (this.Midrails.Count > 0) { gParts.Midrails = this.Midrails.GroupParts(option); }
...///still more code but omitted
return gParts;
}
---这是我所说的用Task.Run(()=>{});
public async Task<IGroupedParts> GroupParts(GroupOption option)
{
return await Task.Run(() =>
{
IGroupedParts gParts = new GroupedParts();
if (this.Doors.Count > 0) { gParts.Doors = this.Doors.GroupParts(option); }
if (this.Leafs.Count > 0) { gParts.Leafs = this.Leafs.GroupParts(option); }
if (this.Sidelites.Count > 0) { gParts.Sidelites = this.Sidelites.GroupParts(option); }
return gParts;
});
}
---我没有并行示例,这就是我寻求帮助的原因...