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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:07:27+00:00 2026-06-07T23:07:27+00:00

I am trying to wire up an AsyncController so that when a user clicks

  • 0

I am trying to wire up an AsyncController so that when a user clicks save on an order on the order page, all users viewing the same order should get a notification that the order has changed. My approach to implement this is to do long polling ajax requests on the order page, however how to make a scalable AsyncController to deal with this is not obvious to me.

So this is what I have so far, the ID is the ID of the order that is signaled as changed or polled for changes.

public class MessageController : AsyncController
{
    static readonly ConcurrentDictionary<int, AutoResetEvent> Events = new ConcurrentDictionary<int, AutoResetEvent>();

    public ActionResult Signal(int id)
    {
        AutoResetEvent @event;
        if (Events.TryGetValue(id, out @event))
            @event.Set();

        return Content("Signal");
    }

    public void WaitAsync(int id)
    {
        Events.TryAdd(id, new AutoResetEvent(false));

        // TODO: This "works", but I should probably not block this thread.
        Events[id].WaitOne();
    }

    public ActionResult WaitCompleted()
    {
        return Content("WaitCompleted");
    }
}

I have had a look at How to do long-polling AJAX requests in ASP.NET MVC? . I am trying to understand all details about this code but as far as I understand this code it is blocking each worker thread in the thread pool which, as far as I understand would eventually lead to thread starvation.

So, how should I implement this in a nice, scalable way? Bear in mind that I do not wish to use any more third party components, I want to get a good understanding of how to implement this scenario properly.

  • 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-07T23:07:30+00:00Added an answer on June 7, 2026 at 11:07 pm

    Actually I was able to implement this without blocking the worker threads, the thing I was missing was ThreadPool.RegisterWaitForSingleObject.

    public class ConcurrentLookup<TKey, TValue>
    {
        private readonly Dictionary<TKey, List<TValue>> _lookup = new Dictionary<TKey, List<TValue>>();
    
        public void Add(TKey key, TValue value)
        {
            lock (_lookup)
            {
                if (!_lookup.ContainsKey(key))
                    _lookup.Add(key, new List<TValue>());
    
                _lookup[key].Add(value);
            }
        }
    
        public List<TValue> Remove(TKey key)
        {
            lock (_lookup)
            {
                if (!_lookup.ContainsKey(key))
                    return new List<TValue>();
    
                var values = _lookup[key];
                _lookup.Remove(key);
    
                return values;
            }
        }
    }
    
    [SessionState(SessionStateBehavior.Disabled)]
    public class MessageController : AsyncController
    {
        static readonly ConcurrentLookup<int, ManualResetEvent> Events = new ConcurrentLookup<int, ManualResetEvent>();
    
        public ActionResult Signal(int id)
        {
            foreach (var @event in Events.Remove(id))
                @event.Set();
    
            return Content("Signal " + id);
        }
    
        public void WaitAsync(int id)
        {
            AsyncManager.OutstandingOperations.Increment();
    
            var @event = new ManualResetEvent(false);
    
            Events.Add(id, @event);
    
            RegisteredWaitHandle handle = null;
            handle = ThreadPool.RegisterWaitForSingleObject(@event, (state, timeout) => 
            {
                handle.Unregister(@event);
                @event.Dispose();
    
                AsyncManager.Parameters["id"] = id;
                AsyncManager.Parameters["timeout"] = timeout;
                AsyncManager.OutstandingOperations.Decrement();
            }, null, new TimeSpan(0, 2, 0), false);
        }
    
    
        public ActionResult WaitCompleted(int id, bool timeout)
        {
            return Content("WaitCompleted " + id + " " + (timeout? "Timeout" : "Signaled"));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to wire up Spring Data JPA objects manually so that I can
Just trying to get my head round Spring and figuring out how I wire
I'm trying to wire up a CheckBox to handle an event when check/unchecked. If
I'm trying to wire up Windsor 3.0 to inject connection strings from my web.config
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
Trying to get a wildcard search to pick up on any text in org_name
Trying to implement LoaderManager + CursorLoader. In onFinish method adapter should swap its cursor
I am trying to wire up a new event, but for some reason Changed
I have been trying to create a Spring project that uses MyBatis for the
So I'm trying to work out how to design my wire-frame. It is essentially

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.