Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
4.08/5 (3 votes)
See more:
Hi
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.
C#
foreach(var file in files)
{
   // .....
   await blockBlob.UploadFromStreamAsync(fs);
   listOfMovedLabelFiles.Add(fileName);
}

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

Is there any way to wait till all the async operations get completed.
Posted
Updated 5-Jun-14 4:52am
v6
Comments
Sampath Lokuge 5-Jun-14 7:03am    
Is this Asp.net forms or MVC ?
Vi(ky 5-Jun-14 7:04am    
It is console application.
Herman<T>.Instance 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.
Herman<T>.Instance 5-Jun-14 18:15pm    
what is the problem for you, the async or the event?

1 solution

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)
  {
    try
    {
      return AsyncContext.Run(() => MainAsync(args));
    }
    catch (Exception ex)
    {
      Console.Error.WriteLine(ex);
      return -1;
    }
  }

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


Read for more info : Async Console Programs
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900