title: Await multiple async Task while setting max running task at a time
body: <p>So I just started to try and understand async, Task, lambda and so on, and I am unable to get it to work like I want. With the code below I want for it to lock btnDoWebRequest, do a unknow number of WebRequests as a Task and once all the Task are done unlock btnDoWebRequest. However I only want a max of 3 or whatever number I set of Tasks running at one time, which I got partly from <a href="https://stackoverflow.com/questions/14075029/have-a-set-of-tasks-with-only-x-running-at-a-time">Have a set of Tasks with only X running at a time</a>. </p>
  
  <p>But after trying and modifying my code in multiple ways, it will always immediately jump back and reenabled btnDoWebRequest. Of course VS is warning me about needing awaits, currently at ".ContinueWith((task)" and at the async in "await Task.WhenAll(requestInfoList .Select(async i =>", but can't seem to work where or how to put in the needed awaits. Of course as I'm still learning there is a good chance I am going at this all wrong and the whole thing needs to be reworked. So any help would be greatly appreciated.</p>
  
  <p>Thanks </p>
  
  <pre><code>    private SemaphoreSlim maxThread = new SemaphoreSlim(3);
      private void btnDoWebRequest_Click(object sender, EventArgs e)
      {
          btnDoWebRequest.Enabled = false;
          Task.Factory.StartNew(async () =&gt; await DoWebRequest()).Wait();
          btnDoWebRequest.Enabled = true;
      }
  
      private async Task DoWebRequest()
      {
          List&lt;requestInfo&gt; requestInfoList = new List&lt;requestInfo&gt;();
          for (int i = 0; dataRequestInfo.RowCount - 1 &gt; i; i++)
          {
              requestInfoList.Add((requestInfo)dataRequestInfo.Rows[i].Tag);
          }
          await Task.WhenAll(requestInfoList .Select(async i =&gt; 
          {
              maxThread.Wait();
              Task.Factory.StartNew(() =&gt;
              {
                  var task = Global.webRequestWork(i);
              }, TaskCreationOptions.LongRunning).ContinueWith((task) =&gt; maxThread.Release());
          }));
      }
  </code></pre>