Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class Program
{
static void Main(string[] args)
{
var timeExecution = new Stopwatch();
Console.WriteLine($"Iniciando a execução");
timeExecution.Start();
InitProcess();
timeExecution.Stop();
Console.WriteLine($"Execução finalizada: {timeExecution.Elapsed:m\\ss\\.fff}");
Console.ReadKey();
}
static void InitProcess()
{
var semaphore = new SemaphoreSlim(1);
var listTasks = new List<Task>();
for (int i = 0; i < 20; i++)
{
listTasks.Add(ProcessWithSemaphore(semaphore));
//listTasks.Add(ProcessSemaphoreless());
}
Task.WaitAll(listTasks.ToArray());
}
static async Task ProcessWithSemaphore(SemaphoreSlim semaphoreSlim)
{
await semaphoreSlim.WaitAsync();
Console.WriteLine("Simulando a chamada a uma API qualquer com semaphore");
var _httpClient = HttpClientFactory.Create();
await _httpClient.GetAsync("http://httpstat.us/200?sleep=1000");
semaphoreSlim.Release();
}
static async Task ProcessSemaphoreless()
{
Console.WriteLine("Simulando a chamada a uma API qualquer sem semaphore");
var _httpClient = HttpClientFactory.Create();
await _httpClient.GetAsync("http://httpstat.us/200?sleep=1000");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment