Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!