title: Run multiple instances of same method asynchronously?
body: <p>My requirement is quite weird. </p>
  
  <p>I have <code>SomeMethod()</code> which calls <code>GetDataFor()</code>. </p>
  
  <pre><code>public void SomeMethod()
  {
      for(int i = 0; i&lt;100; i++) {
          var data = GetDataFor(i);
      }
  }
  
  public data GetDataFor(int i) {
      //call a remote API
      //to generate data for i
      //store to database
      return data;
  }
  </code></pre>
  
  <p>For each <code>i</code>, the end result will <strong>always</strong> be different. There is no need <em>to wait</em> for <code>GetDataFor(i)</code> to complete before calling <code>GetDataFor(i+1)</code>.</p>
  
  <p>In other words I need to:</p>
  
  <ul>
  <li>call <code>GetDataFor()</code> for each <code>i+1</code> <strong>immediately</strong> after successfully calling <code>i</code> (Calling them in parallel looks impossible)</li>
  <li>wait until <strong>all</strong> the 100 instances of <code>GetDataFor()</code> are completed running</li>
  <li>leave the scope of <code>SomeMethod()</code></li>
  </ul>
  
  <p>Following <a href="https://stackoverflow.com/a/16728712/2404470">YK1's answer</a>, I have tried to modify it like this:</p>
  
  <pre><code>public async Task&lt;void&gt; SomeMethod()
  {
      for(int i = 0; i &lt; 100; i++) {
          var task = Task.Run(() =&gt; GetDataFor(i));
          var data = await task;
      }
  }
  </code></pre>
  
  <p>It didn't thrown any errors but I need to understand the concept behind this:</p>
  
  <ul>
  <li>How <code>task</code> will distinguish between different calls for <code>await</code>ing? It is getting over-written.</li>
  <li>Is it blatantly wrong way to do this? So, how do do it right?</li>
  </ul>