0

So here's my problem:

I have a static list of objects that stores all the files being uploaded.

private static List<Upload> vidUploadList = new List<Upload>();

To maintain the list, any uploads that have failed should be removed from the list. My question is: if a user changes the list, the index of items in the list will change. Is it possible for another user to be half way through a list using a loop command and have the list change? If say, the item [3] in the list was being used and then the object before was removed (changing the position to [2]) would the code still run?

Is it possible to lock the list when any user comes to use it and then release the lock straight after, ensuring that only one session at a time is accessing the list? I have heard that the monitor class may help me here.

Christopher Vickers
  • 1,773
  • 1
  • 14
  • 18
  • `static` and web do not mix except for global items. You probably do not want this to be static because all users will be sharing that same collection. – TyCobb Oct 11 '16 at 17:05
  • 1
    Yes, if you're simultaneously reading and writing from the list, things are going to get messed up pretty quickly. You could use a collection from [`System.Concurrent.Collections`](https://msdn.microsoft.com/en-us/library/system.collections.concurrent(v=vs.110).aspx), or better yet, abandon all state in the webserver and use a database. – spender Oct 11 '16 at 17:07
  • Notice that all Informationen inside the list will be gone if your ApplicationPool will disposed – HeW Oct 11 '16 at 17:08
  • I was trying to keep this as simple as I could. The application is used for uploading huge files 20GB+ to a database. I have to keep a SqlFilestream open the whole time, otherwise smaller files will created and swamp my database filestream. The list is basically the SqlFilestream connection of all open connections, which is then used to insert each small chunk of information. As there is potentially hundreds of users I need to have a single list. – Christopher Vickers Oct 11 '16 at 17:11
  • So, at the moment I am looking at the System.Threading.Monitor to restrict access to the list so that only one thread uses it at any one time. This may be slower but will maintain the integrity of the list – Christopher Vickers Oct 11 '16 at 17:20

2 Answers2

1

would the code still run?

NO, an InvalidOperationException "Collection was modified; enumeration operation may not execute" would be thrown

you can actually try it with this simple code

List<int> test = new List<int>(){ 1, 2, 3, 4, 5};
foreach(int i in test)
    test.Remove(i); //Exception

If you really wish to allow multiple users to edit it then consider using a ConcurrentCollection such as a ConcurrentBag

Steve
  • 11,696
  • 7
  • 43
  • 81
-1

When I used C# in Visual Studio 2013, the compiler would complain if it was obvious that the list was being changed while looping through it using foreach. But if you find the size of the list and loop through it using a for loop, I don't think the compilation will fail. But it is still not good practice because the execution could be erratic. Here is a link to how to lock your list.

Community
  • 1
  • 1
Libby
  • 932
  • 8
  • 15