Foreach into Parallel.Foreach c#

Alex

I have the following foreach loop and want to paralise it to improve the speed at which my application reads a list of .csv files. Any help in converting this into a lambda would be very much appreciated!

foreach (var storeData in storeCodesData)

  {
      string[] storeDataSplit = storeData.Split(',');
      if (storeDataSplit[0] != "")
      {
          Store store1 = new Store { StoreCode = storeDataSplit[0], StoreLocation = storeDataSplit[1] };
          stores.Add(store1);
          storename.Add( storeDataSplit[1]);
      }
      PopulateListViewItems(storeDataSplit[0]);
  }  
Hung Cao

I am not sure what are you asking for but based on your question, here is how you can do that. Keep in mind that your logic inside must be "paralleled-ready".

Parallel.ForEach(storeCodesData, (storeData) => 
      {
      string[] storeDataSplit = storeData.Split(',');
      if (storeDataSplit[0] != "")
      {
          Store store1 = new Store { StoreCode = storeDataSplit[0], StoreLocation = storeDataSplit[1] };
          stores.Add(store1);
          storename.Add( storeDataSplit[1]);
      }
      PopulateListViewItems(storeDataSplit[0]);
      });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related