locked
Parallel foreach c# 4.0 RRS feed

  • Question

  • Dear all,

    I have in a datatable (dt ) some informations  that I have to match with another datatable

    (dsSparato.Tables["riscontro"]) .

    Then I wrote the piece of code below

     

    /* CODE */

     foreach (DataRow rowspar in dsSparato.Tables["riscontro"].Rows)
                    {
                                           
                        var selectedRows = from r in dt.AsEnumerable()
                                           where r["barcode"].ToString().Trim().Equals(rowspar["barcode"].ToString().Trim()) && r["codicearticolo"].ToString().IndexOf("ZCOL") == -1
                                           select r;

                       
                        foreach (DataRow row in selectedRows)
                        {

                            rowspar["colore"] = row["colore"];
                            rowspar["codicearticolo"] = row["codicearticolo"];

                        }

                     

                    }

     

    /*end code*/

    I would like to parallelize it.

     

    then I tried to do the follow

     


       Parallel.ForEach<DataRow>(dsSparato.Tables["riscontro"].Rows.OfType<DataRow>(), delegate(DataRow rowspar)
                       {

                           
                            var query = dt.AsEnumerable(); // ERROR!!!

          
                        });

     

    but when I try to compile it

     

    I get the follow error

     

    Error    22    Cannot use ref or out parameter 'dt' inside an anonymous method, lambda expression, or query expression

     

    Or is there another solution to optimize it ?

    Any help would be appreciated

     

    Thank you

     

    Wednesday, September 1, 2010 11:15 AM

Answers

  • It looks like you want to use the data row collection as the Values property of the ParallelForEach, rather than accessing it directly- Can you try setting up your workflow in the designer?

    --Drag a ParallelForEach onto the designer surface of a new Activity (xaml)

    --Create a new Argument of the type of the data collection you're using (it has to implement IEnumerable).

    --Assign the Type parameter of the ParallelForEach to DataRow

    --Assign the Values parameter of the ParallelForEach to the collection argument you created

    This should do it, and handle the various delegate transforms that need to be done.

    Also, keep in mind that the Parallel and ParallelForEach activities don't execute each iteration on a new thread- all of the iterations are scheduled and execute in order, except in the case where one branch of execution goes idle and surrenders thread control, in which case further iterations will be executed while the previous iterations are idle.

    So, an iteration only executes on a new thread if it goes idle and then resumes- if no execution branches go idle, ParallelForEach runs exactly like ForEach.

    Wednesday, September 1, 2010 5:52 PM

All replies

  • It looks like you want to use the data row collection as the Values property of the ParallelForEach, rather than accessing it directly- Can you try setting up your workflow in the designer?

    --Drag a ParallelForEach onto the designer surface of a new Activity (xaml)

    --Create a new Argument of the type of the data collection you're using (it has to implement IEnumerable).

    --Assign the Type parameter of the ParallelForEach to DataRow

    --Assign the Values parameter of the ParallelForEach to the collection argument you created

    This should do it, and handle the various delegate transforms that need to be done.

    Also, keep in mind that the Parallel and ParallelForEach activities don't execute each iteration on a new thread- all of the iterations are scheduled and execute in order, except in the case where one branch of execution goes idle and surrenders thread control, in which case further iterations will be executed while the previous iterations are idle.

    So, an iteration only executes on a new thread if it goes idle and then resumes- if no execution branches go idle, ParallelForEach runs exactly like ForEach.

    Wednesday, September 1, 2010 5:52 PM
  • you can use this 

                Parallel.ForEach(tableToFilter.AsEnumerable(), nextRow =>

    {

    // here nextRow is the DataRow type that holds the row of tableToFilter DataTable.

    });


    yawar

    Thursday, January 13, 2011 12:36 PM
  • Hello,

    Considering that many developers in this forum ask how to Parallel activity in WorkFlow, my team has created a code sample for this frequently asked programming task in Microsoft All-In-One Code Framework. You can download the code samples at:

     

    CSWF4Parallel

     

    http://bit.ly/CSWF4Parallel

    With these code samples, we hope to reduce developers’ efforts in solving the frequently asked

    programming tasks. If you have any feedback or suggestions for the code samples, please email us: onecode@microsoft.com.

    ------------

    The Microsoft All-In-One Code Framework (http://1code.codeplex.com) is a free, centralized code sample library driven by developers' needs. Our goal is to provide typical code samples for all Microsoft development technologies, and reduce developers' efforts in solving typical programming tasks.

    Our team listens to developers’ pains in MSDN forums, social media and various developer communities. We write code samples based on developers’ frequently asked programming tasks, and allow developers to download them with a short code sample publishing cycle. Additionally, our team offers a free code sample request service. This service is a proactive way for our developer community to obtain code samples for certain programming tasks directly from Microsoft.

    Thanks

    Microsoft All-In-One Code Framework

    Wednesday, March 23, 2011 9:43 AM