locked
Problem Looping controls using Parallel.foreach RRS feed

  • Question

  • Hello,
    i'm trying to loop through all my controls using Parallel.Foreach but i'm having some problems... I thought I was going to have no problems cause the ControlCollection class implements IEnumerable...
    This is what I do without Parallel:

    foreach (Control c in ascx.Controls)
    {
      if (c is Dynamic.Controls.UnoN && UnaNaAcumulados.IndexOf(c.ID+",")==-1)
      {
        UnaNaAcumulados += c.ID + ",";
        Control caux = (Control)this.LoadControl("~/Datos/UaN_" + c.ID + ".ascx");
        UaNNames(caux);
      }
    }

    And I'm trying to do the same with parallel, something like this:

    Parallel.ForEach<Control>(ascx.Controls, c => {
      if (c is Dynamic.Controls.UnoN) {
        UnaNaAcumulados += c.ID + ",";
        Control caux = (Control)this.LoadControl("~/Datos/UaN_" + c.ID + ".ascx");
        UaNNames(caux);
      }
    });

    But it says that ControlCollection cannot be converted to OrderablePartitioner.
    Any ideas?? I've been looking for some help but i'm stuck.
     
    Thanks a lot in advance.

    Tuesday, June 8, 2010 11:20 AM

Answers

All replies

  • Hi,

    Try the following:

    using System.Linq;
    Parallel.ForEach<Control>(ascx.Controls.Cast<Control>(), ...);

    See the following link for more info (unfortunately, the specific error message you ran into is not listed in the post):

    http://blogs.msdn.com/b/pfxteam/archive/2010/03/02/9971804.aspx

    Danny

    • Proposed as answer by Danny Shih Tuesday, June 8, 2010 7:49 PM
    • Marked as answer by wildchildbcn Wednesday, June 9, 2010 7:14 AM
    Tuesday, June 8, 2010 7:47 PM
  • It works!!!

     

    Thanks a lot!!

    I think i should learn a lot of things from linq libraries :)

    Wednesday, June 9, 2010 7:15 AM