0

In my project, i have to do something much much times in a foreach loop which i need to wait it about 6-7 hours.

for example: i have 80 docs to check => about 8 hours

foreach (var doc in docs) { bool valid = checkIsValid(doc); // about 2 min per doc if (vaid) { doThing(doc); // about 5 min per doc } else { delete(doc); // about 20s per doc } } 

So, i consider can i check these things in the same time which i have lots of resources is not using when running the above code. And i find C# has a multithread feature (i don't know what can it provides).

Can i use it in my project? How to use it in my project?

ideal: 80 docs are checking in the same time and the foreach loop will finish when 80 threads have died.

foreach (var doc in docs) { //new thread start bool valid = checkIsValid(doc); // about 2 min per doc if (vaid) { doThing(doc); // about 5 min per doc } else { delete(doc); // about 20s per doc } //thread die } 
4
  • 1
    Have you tried Parallel.ForEach (msdn.microsoft.com/en-us/library/…)? Commented Sep 3, 2016 at 9:47
  • @DovydasSopa No, i have not. i will check it. Thanks Commented Sep 3, 2016 at 9:50
  • 1
    80 threads will hog the CPU unless you have 80 cores. you should learn multithreading before you attempt anything, it's very easy to get it wrong. Also, have you checked the complexity of your actual algorithms? 20 seconds to delete something seems huge. Commented Sep 3, 2016 at 9:51
  • Ok from your comments on the answers I gather this is processing over the network. You don't need parallelism, you need asynchrony. You could probably process these 80 docs in parallel with a single thread (threads are primarily for CPU-intensive tasks). Read about async/await. Can't say more if you don't post the real code. Commented Sep 3, 2016 at 10:46

2 Answers 2

2

Have you looked at Parallel.ForEach? Your code would look like:

Parallel.ForEach(docs, doc => { if (checkIsValid(doc)) doThing(doc); else delete(doc); }); 
Sign up to request clarification or add additional context in comments.

4 Comments

what resource it uses? Lucas said multithreading will cost CPU resource.. How about parallel?
Same CPU and memory resources. Just you won't need to write that much of the code your self.
Is it dangerous if i trying to download some thing from internet (3~4 MB) in Parralel.ForEach loop?
@kingyau Not as much as I know. The only this that you should think about is that checkIsValid, doThing and delete would be thread safe.
1

There are multiple ways to achieve concurrency in C# but the time taken by each method depends on the number of cores you have. 'Parallel.ForEach' is one option and probably the simplest way to achieve, but note that it will try to break your work item where possible and will speed up the execution.

The following code is to have multi threading but be careful 80 threads might be an overkill. So create tasks (threads) with proper consideration

foreach (var doc in docs) { //new thread start bool valid = checkIsValid(doc); // about 2 min per doc if (vaid) { Task.Factory.StartNew(() => doThing(doc); } else { delete(doc); // about 20s per doc } //thread die } 

Tasks work in framework 4 and above

4 Comments

Is your solution better than using Parallel.ForEach if doThing(string doc) is (downloading file from network => edit it => replace it ) and delete(string doc) is deleting the doc on host site.
There is always a debate between Parallel.ForEach and Tasks. The way i see it is, the first one is done automatically and the Tasks is done by the user explicitly. You will need to try both the options and monitor the CPU and execution time to make the decision, I have provided you with 2 options to achieve results in quicker way than your current solution.
If my server cannot handle 80 threads in the same time. Can i run apart of them first, then execute others after? for example, only 4 threads run in the same time.
Do you have a development server that you can test both the scenarios, and to anwser your above question. Yes you can certainly split up the execution

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.