Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6215207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:00:49+00:00 2026-05-24T07:00:49+00:00

If I have a Parallel.ForEach like this using a anonymous delegate for the main

  • 0

If I have a Parallel.ForEach like this using a anonymous delegate for the main loop of the ForEach:

var someState = GetSomeState();
Parallel.ForEach(MyIEnumerableSource, 
    () =>
    {
        return new DataTable();
    },
    (record, loopState, localDataTable) =>
    {
        localDataTable.Rows.Add(someState.Value, record);
        return localDataTable
    },
    (localDataTable) =>
    {
        using (var bulkInsert = new SqlBulkCopy(ConnectionString))
        {
            bulkInsert.DestinationTableName = "My_Table";
            bulkInsert.WriteToServer(localDataTable);
        }
        localDataTable.Dispose();
    });

How can I turn it in to the following where the main loop is now a real function but is static sitting in another class:

var someState = GetSomeState();
Parallel.ForEach(MyIEnumerableSource, 
    () =>
    {
        return new DataTable();
    },
    OtherClass.Process,
    (localDataTable) =>
    {
        using (var bulkInsert = new SqlBulkCopy(ConnectionString))
        {
            bulkInsert.DestinationTableName = "My_Table";
            bulkInsert.WriteToServer(localDataTable);
        }
        localDataTable.Dispose();
    });

//In another class in another file
static class OtherClass
{
    public static DataTable Process(MyRecordType record, ParallelLoopState loopState, DataTable localDataTable)
    {
        localDataTable.Rows.Add(someState.Value, record); //How to I bring over someState when it is called here?
        return localDataTable
    }
}

How I can access that state that is created outside of the for loop and does not need to be part of the thread local storage?

I would just use a instance variable if this function was not static and resided in the same class but the main loop of the ForEach is in a static function in another class and will be used from multiple locations in the code, all with their own different copy of someState.

How do I bring that state variable along?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:00:49+00:00Added an answer on May 24, 2026 at 7:00 am

    Short answer: Call it from an anonymous method.
    You can pass your additional state as separate parameter from the anonymous method.

    For example:

    var someState = GetSomeState();
    Parallel.ForEach(MyIEnumerableSource, 
    
        () => new DataTable(),
    
        (record, loopState, localDataTable) =>
           OtherClass.Process(record, loopState, LocalDataTable, someState),
    
        (localDataTable) => { ... }
    );
    
    static class OtherClass
    {
        public static DataTable Process(MyRecordType record, ParallelLoopState loopState, DataTable localDataTable, someStateType someState)
        {
            localDataTable.Rows.Add(someState.Value, record);
            return localDataTable
        }
    }
    

    Long answer: Make a separate class that holds the state, and put the method in that class. You can then pass the instance method, and it will have access to the state from the class that holds it.
    This is how anonymous methods are compiled.

    For a more thorough explanation, see my blog.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an IronPython script that uses the TPL and Parallel.ForEach to process files
I like the simplicity of the Parallel.For and Parallel.ForEach extension methods in the TPL.
I have a bunch of projects in parallel subdirectories that all have etc/lighttpd.conf files.
Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you managed to get Aptana Studio debugging to work? I tried following this,
I am using the task parallel library from .NET framework 4 (specifically Parallel.For and
Let's say I have 2 lists in Python and I want to loop through
Some APIs, like the WebClient, use the Event-based Async pattern . While this looks
Have you ever seen any of there error messages? -- SQL Server 2000 Could

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.