title: Creating a task inside a foreach loop
body: <p>I have a typical foreach loop that calls a method where the parameter is an element of the collection we're looping over; something like this:</p>
  
  <pre><code>foreach (byte x in SomeCollection)
  {
     SomeMethod(x);
  }
  </code></pre>
  
  <p>The problem is that <code>SomeMethod</code> takes a long time to run. I want to move the call into a new task so that the loop just creates the tasks and then the thread that called the loops just continues. How do I do this in a thread-safe way?</p>
  
  <h2>Edit</h2>
  
  <p>I had a performance issue because <code>SomeMethod</code> makes several DB calls. So I converted the loop to a <code>Parallel.ForEach</code> but that didn't make much of a difference because each thread then call the DB. What I'm looking to do is just create Tasks that will <strong>run in the background</strong> and let the main thread continue.</p>