Jump to content

Parallel for each confusion C#

zappian

So I want to iterate trought a dicitonary using a parallel for each.

Which is the source collection and which is the item ?

My code is like this:

List <int> procura(Dictionary <int, string> dicionario, string padrao){	List <int> rout = new List<int>;	Parallel.ForEach(dicionario, d=> 	{		bool result  = Task.Factory.StartNew(() =>contem(d.Item2,padrao);		if(result == true)		{			rout.add(d.Item1);		}	});	return rout;}

I am using this as reference:

https://msdn.microsoft.com/en-us/library/dd537608.aspx

Link to comment
Share on other sites

Link to post
Share on other sites

Which is the source collection and which is the item ?

 

The source collection is dicionario and the item is d

Link to comment
Share on other sites

Link to post
Share on other sites

You're likely to have high contention rate (many attempts from threads trying to get locks). Try using ConcurrentDictionary instead.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×