Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7511295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:30:30+00:00 2026-05-29T23:30:30+00:00

Specifically, I’m wondering: Will the ManualResetEvent consume resources while it is in a wait

  • 0

Specifically, I’m wondering:

Will the ManualResetEvent consume resources while it is in a wait state? Does the performance degradation of context switching apply to threads that are in a wait state?

If I have a choice to use multiple BackgroundThreadQueues that do less work each, or one BackgroundThreadQueue that does more work, and I choose to use multiple…will the waiting thread queues affect process performance while they are not doing anything?

Is there a better FIFO thread queue I should be using in C#, or a different locking strategy?

Any suggestions are appreciated.

/// <summary>
/// This class is responsible for peforming actions in a FIFO order on a 
/// background thread. When it is constructed, a background thread is created 
/// and a manual reset event is used to trigger actions to be performed when 
/// a new action is enqueued, or when one finishes. There is a ShuttingDown 
/// flag that is set by calling code when it is time to destroy the thread, 
/// and a QueueIsEmpty event is fired whenever the queue finishes executing 
/// the last action.
/// </summary>
public class BackgroundThreadQueue : IBackgroundThreadQueue
{
    #region Fields

    private readonly Queue<Action> queueOfActions = new Queue<Action>();
    readonly ManualResetEvent resetEvent;
    private bool shuttingDown;
    private bool readyToShutdown;
    private readonly object lockObject = new object();
    private string queuName;

    #endregion Fields

    #region Events

    /// <summary>
    /// Occurs when the BackgroundThreadQueue is empty, and ready to shut down.
    /// </summary>
    public event EventHandler IsReadyToShutdown;

    #endregion Events

    #region Constructor

    public BackgroundThreadQueue(string threadName)
    {
        this.resetEvent = new ManualResetEvent(false);
        queuName = threadName;
        StartThread();
    }

    #endregion Constructor

    #region Public Methods

    public void ClearQueue()
    {
        lock (lockObject)
        {
            queueOfActions.Clear();
        }
        resetEvent.Set();
    }

    /// <summary>
    /// Enqueues an action, and calls set on the manual reset event to trigger 
    /// the action to be performed (if no action is currently being performed, 
    /// the one just enqueued will be done immediately, if an action is already 
    /// being performed, then the one just enqueued will have to wait its turn).
    /// </summary>
    public void EnqueueAction(Action actionToEnqueue)
    {
        if (actionToEnqueue == null)
        {
            throw new ArgumentNullException("actionToEnqueue");
        }

        bool localReadyToShutDown = false;
        lock (lockObject)
        {
            queueOfActions.Enqueue(actionToEnqueue);

            if(this.readyToShutdown)
            {
                localReadyToShutDown = true;
                this.readyToShutdown = false;
            }
        }

        //if this instance is ready to shut down...and we just enqueued a 
        //new action...we can't shut down now...
        if (localReadyToShutDown)
        {
            StartThread();
        }
        resetEvent.Set();
    }

    #endregion Public Methods

    #region Public Properties

    public bool ReadyToShutdown
    {
        get
        {
            lock (lockObject)
            {
                return this.shuttingDown && this.readyToShutdown;
            }
        }
        private set
        {
            this.readyToShutdown = value;
            if (this.readyToShutdown)
            {
                //let interested parties know that the queue is now empty 
                //and ready to shutdown
                IsReadyToShutdown.Raise(this);
            }
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether or not the queue should shut down 
    /// when it is finished with the last action it has enqueued to process.
    /// If the queues owner is shutting down, it needs to notify the queue,
    /// and wait for a QueueIsEmpty event to be fired, at which point the reset 
    /// event will exit ... the owner shouldn't actually destroy the queue 
    /// until all actions have been performed.
    /// </summary>
    public bool ShuttingDown
    {
        get
        {
            lock (lockObject)
            {
                return this.shuttingDown;
            }
        }
        set
        {
            lock (lockObject)
            {
                bool startThread = false;
                if (value == false)
                {
                    readyToShutdown = false;
                    //if we were shutting down...but, now are not
                    startThread = this.shuttingDown;
                }

                this.shuttingDown = value;

                //if we were shutting down, but now are not...
                //we need to restart the processing actions thread
                if (startThread)
                {
                    StartThread();
                }
            }

            this.resetEvent.Set();
        }
    }

    #endregion Public Properties

    #region Private Methods

    private void StartThread()
    {
        var processActionsThread = new Thread(this.ProcessActions);
        processActionsThread.Name = queuName;
        processActionsThread.IsBackground = true;
        processActionsThread.Start();            
    }

    /// <summary>
    /// Processes the actions in a while loop, resetting a ManualResetEvent that 
    /// is triggered in the EnqueueAction method and ShuttingDown property.
    /// </summary>
    private void ProcessActions()
    {
        while (true)
        {
            Action action = null;
            lock (lockObject)
            {
                //if there are any actions, then get the first one out of the queue
                if (queueOfActions.Count > 0)
                {
                    action = queueOfActions.Dequeue();
                }
            }
            if (action != null)
            {
                action();
            }
            lock (lockObject)
            {
                //if any actions were added since the last one was processed, go 
                //back around the loop and do the next one
                if (this.queueOfActions.Count > 0)
                {
                    continue;
                }

                if (this.shuttingDown)
                {
                    //ReadyToShutdown setter will raise IsReadyToShutdown
                    ReadyToShutdown = true;
                    //get out of the method if the user has chosen to shutdown, 
                    //and there are no more actions to process
                    return;
                }                    
                this.resetEvent.Reset();
            }

            this.resetEvent.WaitOne();
        }
    }

    #endregion Private Methods
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:30:31+00:00Added an answer on May 29, 2026 at 11:30 pm

    The mere existence of waiting threads should have no ongoing performance impact beyond the memory reserved to house the thread’s actual stack, and whatever accounting information the runtime or kernel keeps regarding them. So no, truly waiting threads aren’t going to do anything except consume RAM.

    That said, I’m not sure why you’d write this code, as .Net has a threadpool built in, and concurrency tools that you should probably prefer over your own.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Specifically how can I: Show a button which will let the user browse through
specifically, will working with containers as opposed to the static ObjectFactory let me keep
Specifically, in the case of object reference equality, what does the == operator do?
Specifically, I'm trying to figure out if it's possible to generate SQL that does
Specifically, I'm wondering which of these I should write: { shared_ptr<GuiContextMenu> subMenu = items[j].subMenu.lock();
Specifically im wondering if in java (preferred) or perl is it possible to enter
Specifically, when I return a connection to the pool, does dbcp (and other connection
Specifically the error occurs in the Resources.Designer.cs : Error 2 The namespace 'ModulusFE' already
Specifically, I need to validate the incoming X.509 security certificate against a database to
Specifically I am interested for the LAMP Stack and some of the best practices

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.