SoFunction
Updated on 2025-03-07

C# How to use semaphore to manage asynchronous download requests

This article describes how C# uses semaphore to manage asynchronous download requests. Share it for your reference. The specific implementation method is as follows:

var semaphor = new Semaphore(50, 50);
// We allow at most 50 threads for crawling
var resultPins = new List<Pin>();
// Results stored here
foreach (var pin in new HashSet<string>(pinIdList))
{
  ();
  (">");
  var pinClient = new WebClient();
   += (sender, ex) =>
  {
   var html = ("\n", "");
   ();
   lock (pinIdList)
   {
     // do some post-processing and write back the results
   }
   ("<");
   ();
  };
  (new Uri(("/pin/{0}/", pin)));
}
for (int i = 0; i < 50; i++) ();
// Wait until the last thread ends.
();
();

I hope this article will be helpful to everyone's C# programming.