I'm trying to run multiple functions that connect to a remote site (by network) and return a generic list. But I want to run them simultaneously.
For example:
public static List<SearchResult> Search(string title) { //Initialize a new temp list to hold all search results List<SearchResult> results = new List<SearchResult>(); //Loop all providers simultaneously Parallel.ForEach(Providers, currentProvider => { List<SearchResult> tmpResults = currentProvider.SearchTitle((title)); //Add results from current provider results.AddRange(tmpResults); }); //Return all combined results return results; }
As I see it, multiple insertions to 'results' may happend at the same time... Which may crash my application.
How can I avoid this?
You don't have to do anything special, Parallel. Foreach() will wait until all its branched tasks are complete. From the calling thread you can treat it as a single synchronous statement and for instance wrap it inside a try/catch.
ForEach() is meant to be a parallel Asynchronous operation, so there is nothing to continue; or break; as theoretically all _dongles. GetArray() members should be accessed at the same instance, thus having a break; or especially a continue; won't be logical in the first place. You should just use return; that's all.
The linq parallel foreach will partition your collection and work on it at the same time, which you also need to take into account making sure your this. Update can work with multiple users without messing up .
The execution of Parallel. Foreach is faster than normal ForEach.
You can use a concurrent collection.
The
System.Collections.Concurrent
namespace provides several thread-safe collection classes that should be used in place of the corresponding types in theSystem.Collections
andSystem.Collections.Generic
namespaces whenever multiple threads are accessing the collection concurrently.
You could for example use ConcurrentBag
since you have no guarantee which order the items will be added.
Represents a thread-safe, unordered collection of objects.
//In the class scope: Object lockMe = new Object(); //In the function lock (lockMe) { results.AddRange(tmpResults); }
Basically a lock means that only one thread can have access to that critical section at the same time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With