How to synchronize the access to a List used in ASP.NET?

后端 未结 4 521
北荒
北荒 2021-01-01 03:58

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 m

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-01 04:18

    Yes, you have to lock- using List.SyncRoot like this:

    lock (myList.SyncRoot) 
    {
        // Access the collection.
    }
    

提交回复
热议问题