如何同步访问列表< T>在ASP.NET中使用? [英] How to synchronize the access to a List<T> used in ASP.NET?

查看:141
本文介绍了如何同步访问列表< T>在ASP.NET中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经在一个列表中的并发访问网站的一些问题。这个列表保存物品的购物车,并多次被删除崩溃的部位。
这是同步它们的最好方法是什么?
是锁就够了吗?
锁的选择似乎是丑陋的,因为code为S $ P $垫所有的地方,是pretty凌乱。

I have some problems on a site with the concurrent access to a list. This list keeps a cart of items, and multiple deletes are crashing the site. Which is the best method to sync them? Is a lock enough? The lock option seems to be ugly because the code is spread all over the place and is pretty messy.

更新:
这是这样实现的一个列表:
公共类MYLIST:列表< SomeCustomType> {}

Update: This is a list implemented like this: public class MyList : List< SomeCustomType> { }

这是一个旧的网站,所以没有那么多的修改被允许的。
我应该如何重构这个以迭代它时,安全锁?

This is a legacy site so not so many modifications are allowed to it. How should I refactor this in order to safely lock when iterating over it ?

任何想法!

推荐答案

所以请求之间共享此内存列表?这听起来像的,即使你有锁定问题的潜在原因。可变共享集合一般应避免,IME。

So is this in-memory list shared between requests? That sounds like a potential cause of problems even when you've got locking. Mutable shared collections should generally be avoided, IME.

请注意,如果你的的决定同步,你需要做的事情一样锁定列表遍历它和其他复合操作的全过程。仅仅锁定在每个特定的访问是不够的。

Note that if you do decide to synchronize, you'll need to do things like locking the list for the whole course of iterating over it and other compound operations. Just locking on each specific access isn't good enough.

这篇关于如何同步访问列表&LT; T&GT;在ASP.NET中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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