Is Session variable thread-safe within a Parallel.For loop in ASP.Net page

Sunil

Would changing a session variable (i.e. Session["Progress"]) in code below be safe?

This code is part of code-behind of an ASP.Net page.

When running a loop in parallel, two iterations could run simultaneously and cause issues if the same session variable is changed by both iterations.

   public void LongOperation()
    {
        Parallel.For(0, totalMembers,(i,loopState) =>
        {
            Thread.Sleep(2000);//wait some time to simulate processing
            progress++;
            Session["Progress"] = progress;//IS THIS THREAD-SAFE?
        } 
       ); 
   }
Aristos

This is NOT thread safe, but the session across different open page is safe to change.

So under certain circumstances if you call it is safe.

If you call it only one time from you page, then is safe across different pages. The reason for that is that the session is fully lock the session from the start of page load up to the end.

Some similar answers to prove that.

Web app blocked while processing another web app on sharing same session
Does ASP.NET Web Forms prevent a double click submission?
Trying to make Web Method Asynchronous

If you call it more than one time from the same page, from different threads, then you need synchronization.

Notes

  • If this loop is take long to complete then all users will be wait for that calculations and not only the session user. All users will be lock down by the session lock. If too many users call that then all users together will be wait for long time. So you may need to make this calculations on a scheduler task.
  • If the time to process the data inside the loop is smaller than the time the loop takes to create and synchronize the threads, then the parallel loop is takes more time to complete than the a simple loop. So you need to calculate the speed here for the simple loop and the parallel loop.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is Session variable thread-safe within a Parallel.For loop in ASP.Net page

From Dev

Change session variable and reload page in ASP.NET

From Dev

Change session variable and reload page in ASP.NET

From Dev

ASP.NET MVC Outputcache "thread safe"

From Dev

Is it possible to set localStorage or Session variable in asp.net page and read it in javascript on the other page?

From Dev

Using a session variable in authentification within asp.net mvc 4 application

From Dev

Using a session variable in authentification within asp.net mvc 4 application

From Dev

Is it safe to access a local variable within the context of an anonymous thread

From Dev

Is it safe to access a local variable within the context of an anonymous thread

From Dev

asp.net - how to set a session variable as soon as a new user visit any web page ?

From Dev

ASP.NET Core MVC dropdown list on _layout page to a Session variable

From Dev

how to store Gridview Boolean value in session variable and show another page in asp.net

From Dev

ASP.NET ternary operator on the Session variable

From Dev

ASP.NET ternary operator on the Session variable

From Dev

Thread.CurrentPrincipal.Identity in ASP .NET - is it safe to use

From Dev

Is this code thread safe in ASP.NET MVC 4?

From Dev

Static HttpClient thread safe on ASP.net HttpRequest

From Dev

ASP.NET Web API - Thread safe logic for updating an entity

From Dev

Thread safe or not? Updating a not-thread-safe-map from a parallel stream

From Dev

Method local variable being modified inside a Parallel.For. How thread safe is this?

From Dev

How to transfer session variable from classic asp to asp.net?

From Dev

ASP.NET instantiating regex within a static method and thread safety

From Dev

Thread safe math operations in Parallel on Apache Ant

From Dev

Is it thread-safe to make calls to OkHttpClient in parallel?

From Dev

TestNG: Is running classes in parallel thread safe?

From Dev

Is it safe to create new thread in a loop?

From Dev

Thread safe access to a variable in a class

From Dev

Thread safe access to member variable

From Dev

Is it thread safe to modify a static variable?

Related Related

  1. 1

    Is Session variable thread-safe within a Parallel.For loop in ASP.Net page

  2. 2

    Change session variable and reload page in ASP.NET

  3. 3

    Change session variable and reload page in ASP.NET

  4. 4

    ASP.NET MVC Outputcache "thread safe"

  5. 5

    Is it possible to set localStorage or Session variable in asp.net page and read it in javascript on the other page?

  6. 6

    Using a session variable in authentification within asp.net mvc 4 application

  7. 7

    Using a session variable in authentification within asp.net mvc 4 application

  8. 8

    Is it safe to access a local variable within the context of an anonymous thread

  9. 9

    Is it safe to access a local variable within the context of an anonymous thread

  10. 10

    asp.net - how to set a session variable as soon as a new user visit any web page ?

  11. 11

    ASP.NET Core MVC dropdown list on _layout page to a Session variable

  12. 12

    how to store Gridview Boolean value in session variable and show another page in asp.net

  13. 13

    ASP.NET ternary operator on the Session variable

  14. 14

    ASP.NET ternary operator on the Session variable

  15. 15

    Thread.CurrentPrincipal.Identity in ASP .NET - is it safe to use

  16. 16

    Is this code thread safe in ASP.NET MVC 4?

  17. 17

    Static HttpClient thread safe on ASP.net HttpRequest

  18. 18

    ASP.NET Web API - Thread safe logic for updating an entity

  19. 19

    Thread safe or not? Updating a not-thread-safe-map from a parallel stream

  20. 20

    Method local variable being modified inside a Parallel.For. How thread safe is this?

  21. 21

    How to transfer session variable from classic asp to asp.net?

  22. 22

    ASP.NET instantiating regex within a static method and thread safety

  23. 23

    Thread safe math operations in Parallel on Apache Ant

  24. 24

    Is it thread-safe to make calls to OkHttpClient in parallel?

  25. 25

    TestNG: Is running classes in parallel thread safe?

  26. 26

    Is it safe to create new thread in a loop?

  27. 27

    Thread safe access to a variable in a class

  28. 28

    Thread safe access to member variable

  29. 29

    Is it thread safe to modify a static variable?

HotTag

Archive