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 9151547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:49:47+00:00 2026-06-17T11:49:47+00:00

I have a singleton service class on IIS as part of a web application

  • 0

I have a singleton service class on IIS as part of a web application (the service to be a singleton for data caching reasons). Browser clients that make requests to the service can have one of three results:

1) There is data in the cache and the data is not expired (stale) – we return this data. Very fast.
2) The cached data is expired, but another request is already querying the database. We returned the cached data.
3) The cached data is expired and no requests are making queries to the DB. This request moves forward to make the query.

However, database queries that target stored procedures with the same name have to be queued (requirement).

Thus, I wrote this queue class that is designed to queue these queries and run them consecutively, instead of concurrently. These queue classes are created as needed and stored in a list in the singleton class. When a request moves to part (3), it finds the queue class that matches its stored procedure name and submits the request to the queue class. It then waits until the data is returned from the DB so it can service the HTML request.

Unfortunately, after a few hours with this code in place, the server process maxes at 100%.

I am not sure what the best way is to go about improving it, because multi-thrread coding is not my specialty.

The queue class code looks like this:

public ReportTable GetReportTable(ReportQuery query)
{
  lock (_queue)
  {
    _queue.Enqueue(query);
    Monitor.Pulse(_queue);
  }

  lock (_queue)
  {
    var firstQueryInQueue = _queue.Peek();
    while (_inUse || firstQueryInQueue == null || firstQueryInQueue.GetHashCode() != query.GetHashCode())
    {
      Monitor.Pulse(_queue);
      Monitor.Wait(_queue);
    }

    _inUse = true;
    firstQueryInQueue = _queue.Dequeue();
    var table = firstQueryInQueue.GetNewReportTable();
    _inUse = false;

    Monitor.Pulse(_queue);
    return table;
  }
}
  • 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-06-17T11:49:49+00:00Added an answer on June 17, 2026 at 11:49 am

    So here’s what I did to fix it.

    public ReportTable GetReportTable(ReportQuery query)
    {
      lock (_queue)
      {
        _queue.Enqueue(query);
        Monitor.Pulse(_queue);
      }
    
      lock (_queue)
      {
        var firstQueryInQueue = _queue.Peek();
        while (_inUse || firstQueryInQueue == null || firstQueryInQueue.GetHashCode() != query.GetHashCode())
        {
          Monitor.Wait(_queue);
        }
    
        _inUse = true;
        firstQueryInQueue = _queue.Dequeue();
        var table = firstQueryInQueue.GetNewReportTable();
        _inUse = false;
    
        Monitor.Pulse(_queue);
        return table;
      }
    }
    

    The reason it didn’t work before was because of my lack of total understanding of Monitor.Wait() and Monitor.Pulse(). I was using Pulse() in a wrong place in the code. Fortunately, there’s a very good answer here that goes on to describe Wait() and Pulse() quite well.

    The key is to Pulse() after the collection is changed, and give the subsequent queued threads a chance to test for the condition, which is: Is my query first in the queue? And is someone else already making a query? Failing that test, the thread calls Wait(), which puts it in the wait queue, and it doesn’t tie up processor cycles. When the thread that is in the front and is performing the query completes, it flips the _inUse flag to false and calls Pulse(), waking one of the next threads so it can check the condition.

    After implementing this solution and watching the Task Manager for a day, I was happy to see 1% to 5% load on the server for several hours, and the CPU never climbed to 100% as before.

    I’ve done a lot of reading on this and it seems that PulseAll() might be the better call in this scenario, but so far Pulse() is working and we haven’t encountered any issues.

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

Sidebar

Related Questions

I have a singleton class that will initialize it's data from a web service
I have a Service singleton class with static methods that I call from the
So I have this service class that utilize Restkit to consume REST web services.
In my web application, I have a background service. This service uses Generator class
I have an application that downloads information from a web service and caches it
I have a Singleton that is accessed in my class via a static property
Assume I have a singleton class in an external lib to my application. But
I have a class that implements a plugin for an existing application. I also
I have a small web application that I am building. Primarily to improve my
I have an application that keeps some complex data in memory between activities. As

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.