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 6921545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:17:27+00:00 2026-05-27T10:17:27+00:00

I am using .AsParallel().ForAll() to enumerate a collection in parallel in the context of

  • 0

I am using .AsParallel().ForAll() to enumerate a collection in parallel in the context of an ASP.NET request. The enumeration method relies on System.Threading.Thread.CurrentPrincipal.

Can I rely on the individual threads used to have their System.Threading.Thread.CurrentPrincipal set to the HttpContext.Current.User of the thread that is processing the ASP.NET Request or do I need to manage that myself?

Another way of asking the question is do the threads used by PLINQ inherit the identity of the thread that invoked the operation?

  • 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-27T10:17:28+00:00Added an answer on May 27, 2026 at 10:17 am

    No, the identity will not be propagated to these worker threads automatically. If, in fact, the components you are using are HttpContext.User what you can do is capture the current, “ambient” HttpContext instance in your “main” thread and propagate it to your worker threads. That would look something like this:

    HttpContext currentHttpContext = HttpContext.Current;
    
    myWorkItems.AsParallel().ForAll(wi =>
    { 
        HttpContext.Current = currentHttpContext;
    
        try
        {
            // anything called from here out will find/use the context of your original ASP.NET thread
        }
        finally
        {
           // Disassociate the context from the worker thread so that it is not held on to beyond its official lifetime
           HttpContext.Current = null;
        }
    });
    

    This works because HttpContext.Current is backed by a thread static, so every worker thread will be assigned the instance from your main thread and any work done on it from that point will see that as the current instance.

    Now, you have to be aware that HttpContext and its related classes were not designed to be thread safe, so this is a bit of a hack. If you’re only reading from properties this isn’t really a problem. If you are not using components that rely on HttpContext.Current then it would be “cleaner” to not set that and instead just use the captured currentHttpContext variable directly in the worker.

    Finally, if all you really need is to propagate the current principal to the worker threads then you can do just that instead using the same approach:

    Principal logicalPrincipal = Thread.CurrentPrincipal;
    
    myWorkItems.AsParallel().ForAll(wi =>
    { 
        Principal originalWorkerThreadPrincipal = Thread.CurrentPrincipal;
        Thread.CurrentPrincipal = logicalPrincipal;
    
        try
        {
            // anything called from here out will find the principal from your original thread
        }
        finally
        {
           // Revert to the original identity when work is complete
           Thread.CurrentPrincipal = originalWorkerThreadPrincipal;
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using ASP.NET MVC there are situations (such as form submission) that may require a
Using preview 4 of ASP.NET MVC Code like: <%= Html.CheckBox( myCheckBox, Click Here, True,
I am using the parallel data structures in my .NET 4 application and I
I am using the C# 4.0 AsParallel() extension method and getting an UnAuthorizedAccessException when
Using online interfaces to a version control system is a nice way to have
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters
Using VS2008, C#, .Net 2 and Winforms how can I make a regular Button
I am pretty much interested into using the newly enhanced Parallelism features in .NET
I just wonder is parallel File.Read using PLINQ/Parallel can be faster? My code is

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.