user3447602
user3447602

Reputation: 73

Task inside and outside a loop

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);

            }
        });
}

Upvotes: 0

Views: 132

Answers (1)

Matt Coats
Matt Coats

Reputation: 332

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.

Upvotes: 3

Related Questions