Do I understand correctly that the code below each HandleContextAsync(context))
runs in a new thread? Using the best way to limit the number of threads
private void Listen()
{
while (true)
{
try
{
if (listener.IsListening)
{
var context = listener.GetContext();
sem.WaitOne();
Task.Run(() => HandleContextAsync(context));
}
else
Thread.Sleep(0);
}
}
}
private async Task HandleContextAsync(HttpListenerContext listenerContext)
{
//I process the image received in the request
var iBit = imageBit.Clone(imageInterSect, imageBit.PixelFormat);
iBit.Save(listenerContext.Response.OutputStream, ImageFormat.Png);
listenerContext.Response.ContentType = "image/png";
listenerContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
listenerContext.Response.Close();
}