是Session变量在ASP.Net页面的Parallel.For循环中的线程安全 [英] Is Session variable thread-safe within a Parallel.For loop in ASP.Net page

查看:251
本文介绍了是Session变量在ASP.Net页面的Parallel.For循环中的线程安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

会在code更改会话变量(即会话[进展])下面是安全的?

这code是一个ASP.Net页面code-后面的部分。

在并行运行一个循环,两个迭代可以同时运行,并且如果在同一会话变量由这两个迭代变化引起的问题。

 公共无效LongOperation()
    {
        的Parallel.For(0,totalMembers中,(i,loopState)= GT;
        {
            Thread.sleep代码(2000); //等待一段时间,模拟处理
            进度++;
            会话[进展] =进度; //这是线程安全的?
        }
       );
   }


解决方案

这是不是线程安全,但在不同的打开网页会话是安全的更改

所以,在某些情况下,如果你把它是安全的。

如果你调用它的只有一个从你页面时间,然后就是在不同的页面是安全的。
其原因是,本次会议是完全锁定在页面加载的启动会议到最后。

一些类似的回答来证明。

<一个href=\"http://stackoverflow.com/questions/9426673/web-app-blocked-while-processing-another-web-app-on-sharing-same-session\">Web应用程序被阻止在处理上共享同一个会话结果另一个Web应用程序
<一href=\"http://stackoverflow.com/questions/11629600/does-asp-net-web-forms-$p$pvent-a-double-click-submission/11629664#11629664\">Does ASP.NET Web窗体prevent双击提交?结果
<一href=\"http://stackoverflow.com/questions/11325150/trying-to-make-web-method-asynchronous/11325285#11325285\">Trying使Web方法异步

如果您从同一个页面中调用它一次以上的 ,从不同的线程,然后您需要同步

注释


  • 如果这个循环是需要很长时间才能完成然后点击所有用户都将等待
    该计算
    并不仅仅是会话的用户。所有用户都将被会话锁被锁定。如果有太多的用户调用,那么所有用户一起将等待很长一段时间。所以,你可能需要做出一个调度任务的此计算。

  • 如果以处理循环内的数据的时间小于循环需要创建和同步线程的时候,那么并行循环是需要更多的时间完成比一个简单的循环。所以,你需要在这里计算速度的简单循环和并行循环。

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?
        } 
       ); 
   }

解决方案

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.

这篇关于是Session变量在ASP.Net页面的Parallel.For循环中的线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆