title: Need to understand the usage of SemaphoreSlim
body: <p>Here is the code I have but I don't understand what <code>SemaphoreSlim</code> is doing.</p>
  <pre><code>async Task WorkerMainAsync()
  {
      SemaphoreSlim ss = new SemaphoreSlim(10);
      List&lt;Task&gt; trackedTasks = new List&lt;Task&gt;();
      while (DoMore())
      {
          await ss.WaitAsync();
          trackedTasks.Add(Task.Run(() =&gt;
          {
              DoPollingThenWorkAsync();
              ss.Release();
          }));
      }
      await Task.WhenAll(trackedTasks);
  }
  
  void DoPollingThenWorkAsync()
  {
      var msg = Poll();
      if (msg != null)
      {
          Thread.Sleep(2000); // process the long running CPU-bound job
      }
  }
  </code></pre>
  <p>What do <code>await ss.WaitAsync();</code> and <code>ss.Release();</code> do?</p>
  <p>I guess that if I run 50 threads at a time then write code like <code>SemaphoreSlim ss = new SemaphoreSlim(10);</code> then it will be forced to run 10 active thread at time.</p>
  <p>When one of 10 threads completes then another thread will start. If I am not right then help me to understand with sample situation.</p>
  <p>Why is <code>await</code> needed along with <code>ss.WaitAsync();</code>? What does <code>ss.WaitAsync();</code> do?</p>