Click here to Skip to main content
14,382,476 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to upload files to Azure Blob Storage and after successful upload adding the filename to a list for my further operation. When i am doing synchronous it works fine but when i am doing async the error occured.
Error : Collection was modified; enumeration operation may not execute.
foreach(var file in files)
   // .....
   await blockBlob.UploadFromStreamAsync(fs);

if (listOfMovedLabelFiles.Count > 0) // error point
    // my code for further operation

Is there any way to wait till all the async operations get completed.
Updated 5-Jun-14 5:52am
Sampath Lokuge 5-Jun-14 7:03am
Is this forms or MVC ?
Vi(ky 5-Jun-14 7:04am
It is console application.
digimanus 5-Jun-14 8:06am
because of asynchronity your lists expands in the background. Better do the job in the background and raise an event when a filename must be added to the list
Rahul Kumar 5-Jun-14 10:28am
and how to implement it, any sample or link.
digimanus 5-Jun-14 18:15pm
what is the problem for you, the async or the event?

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

If it's a console app then you have to use Async is as below.

Note: This is just a sample.Adjust it according to your app.

class Program
  static int Main(string[] args)
      return AsyncContext.Run(() => MainAsync(args));
    catch (Exception ex)
      return -1;

  static async Task<int> MainAsync(string[] args)

Read for more info : Async Console Programs

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100