Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

128
Visualizações
AutoResetEvent number of waiting threads .net 5.0

I am using AutoResetEvent. I just need to know is it possible to get number of waiting threads?

if (WaitHandler.Reset())
{
     if (WaitHandler.WaitOne(WaitMilliseconds))
     {
         // do something after WaitHandler.Set()  
     }
}

I need to know how many threads are currently waiting in WaitOne(). And is it possible to limit them, like 10 threads max - all other threads throw back? If not - how to limit it?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I'm not sure if this is helpful, I found it at https://kmyr.dev/post/limiting-the-number-of-threads

internal sealed class ThreadManager
{
private readonly WaitHandle[] _syncObjects;

public ThreadManager() : this(Environment.ProcessorCount)
{
}

public ThreadManager(int maxThreads)
{
    MaxThreads = maxThreads;

    _syncObjects = new WaitHandle[maxThreads];
    for (var i = 0; i < maxThreads; i++)
    {
        _syncObjects[i] = new AutoResetEvent(true);
    }
}

public int MaxThreads { get; private set; }

public bool StartTask(Action action, bool wait)
{
    var timeout = wait ? Timeout.Infinite : 0;
    var freeIndex = WaitHandle.WaitAny(_syncObjects, timeout);
    if (freeIndex == WaitHandle.WaitTimeout)
    {
        return false;
    }

    ThreadPool.QueueUserWorkItem(
        state =>
            {
                action();
                ((AutoResetEvent)state).Set();
            },
        _syncObjects[freeIndex]);

    return true;
}
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda