English 中文(简体)
合并数 与此同时。 C#
原标题:Consolidate several Parallel.ForEach into one in C#

<代码>Parallel.ForEach> 直至所有校对返回为止。 每一物体 以下类型实际上是一份清单。 是否有任何办法使用<代码>Parallel。 每个 在这一情景中,仅仅一次? 请提出咨询意见。

Main(){
    List<Type1> objectType1 = ...
        List<Type2> objectType2 = ...
        List<Type3> objectType3 = ...

    Parallel.ForEach(objectType1, MyFunction)
    Parallel.ForEach(objectType2, MyFunction)
    Parallel.ForEach(objectType3, MyFunction)
}


EDIT: This is MyFunction:

MyFunction (object arg) {

    //some code here

    if (arg is Type1) { ProcessType1(arg as Type1); }

    else if (arg is Type2) { ProcessType2(arg as Type2); }

    else if (arg is Type3) { ProcessType3(arg as Type3); }

    //some more code here
}
最佳回答

You need to concat. The way you are going to do it depends on the type of Type01, Type02 and Type03. I am going to assume they are custom classes, so you can do like this:

public class X { }
public class Y { }
public class Z { }

static void Main(string[] args)
{
    var l1 = new List<X> { new X() };
    var l2 = new List<Y> { new Y() };
    var l3 = new List<Z> { new Z() };

    var master = new List<dynamic>();

    master.AddRange(l1);
    master.AddRange(l2);
    master.AddRange(l3);

    Parallel.ForEach(master,
        val =>
        {
            var isX = val is X;
        });
}

如果你的问题重复了同一职能,那么你可以将职能机构储存在<条码>上。 Action<dynamic>

Action<dynamic> action =
    (val) =>
    {
        var isX = val is X;
    };

And call

Parallel.ForEach(yourList, action);
问题回答

For the pseudo-code you ve written above, Type1, Type2 and Type3 would all have to be convertible to a common type, the type of the parameter to the MyFunction method. If they all have a common base type, and you are really calling MyFunction for all of them then you can combine the sequences using LINQ:

Parallel.ForEach(objectType1.Concat<BaseType>(objectType2).Concat(objectType3),
    MyFunction);

www.un.org/Depts/DGACM/index_spanish.htm looks:

public void MyFunction(BaseType baseType)
{
    // Process base type...
}

你们可以通过投射反对和使用目录来加以合并。

List<object> combined = one.Cast<object>()
    .Concat<object>(two.Cast<object>())
    .Concat(three.Cast<object>())
    .ToList();

我还要指出,采用这样的思考,可能表明一些坏的设计决定。 如果可能的话,你就应当找到不同类型应用的共同接口。 例如:

interface IProcessable
{
    void Process();
}

class Type1 : IProcessable
{
    public void Process(){ //stuff }
}

class Type2 : IProcessable
{
    public void Process(){ //stuff }
}

然后,你就有了<条码>。 缩略语

Parallel.Foreach(listOfStuff, Process);




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签