0

I have two methods below. Can you please tell me what each method do in brief and how does the two methods differ from each other ?

 public void Method1() { foreach (string symbol in arrList) { Task.Factory.StartNew(() => DoWork(symbol)); } } public void Method2() { Task.Factory.StartNew(() => { foreach (string symbol in arrList) { DoWork(symbol); } }); } 
3
  • 2
    You already have an answer - "Task inside and outside a loop". Can you please clarify what you don't understand? Especially after reading documentation on StartNew? Commented Jul 19, 2016 at 17:50
  • Will method2 create separate task for each for-loop or no ?? Commented Jul 19, 2016 at 17:52
  • @user3447602 How many times is DoWork() called in Method2 (inside the loop) and how many times when you just made a single call? If you can answer that you have the answer to your question. Commented Jul 19, 2016 at 18:00

1 Answer 1

3

The first will create many asynchronous Tasks that each call DoWork with the given symbol. The second will create one asynchronous Task that will do the entire loop before exiting.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Is it possible to know if all the tasks in the first method got completed ?
@user3447602 Sure, collect them in a list and pass them to Task.WhenAll.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.