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

  • Home
  • SEARCH
  • 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 8709315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:20:16+00:00 2026-06-13T04:20:16+00:00

I created a static class to hold user messages such as Item successfully added

  • 0

I created a static class to hold user messages such as “Item successfully added” or “Password successfully changed”, etc. Inside the class is a static dictionary which holds all the messages. The key of the dictionary is the UserId. I then have an Action which renders in _Layout.cshtml so that the messages will follow the user even if they are redirected off the page.

For example, I might allow a user to “Add” an item, and then once the item is added successfully, it will redirect to the List page for that item and then display the message “Item successfully added.”

This worked great until I deployed to my production site and I noticed that the messages were “lagging”. I would add an item and then it would redirect to the list page, but the message would not display. I would then navigate to somewhere else in the application and then the message would display on that page.

Any ideas why this would be happening?

Here’s the code for my UserMessageManager

public static class UserMessageManager
{
    private static readonly Dictionary<int, Queue<UserMessage>> UserMessages = new Dictionary<int, Queue<UserMessage>>();

    public static void Add(int userId, string message)
    {
        if (string.IsNullOrWhiteSpace(message))
            return;

        if (!UserMessages.Keys.Contains(userId))
        {
            UserMessages.Add(userId, new Queue<UserMessage>());
        }

        UserMessages[userId].Enqueue(new UserMessage { Message = message});
    }

    public static List<UserMessage> Get(int userId)
    {
        if (!UserMessages.Keys.Contains(userId))
        {
            UserMessages.Add(userId, new Queue<UserMessage>());
        }

        var messages = new List<UserMessage>();
        while (UserMessages[userId].Any())
        {
            messages.Add(UserMessages[userId].Dequeue());
        }

        return messages;
    }
}

public class UserMessage
{
    public string Message { get; set; }
}

EDIT: After some playing around, the messages will sometimes even “bunch up”. I will add a messages to the dictionary after creating a few “items” and the messages will suddenly all display at once at a seemingly random time.

  • 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-13T04:20:18+00:00Added an answer on June 13, 2026 at 4:20 am

    Firstly, if this is for a real site, I wouldn’t take this approach anyway. If you want information persisted, use something persistent (e.g. a database). Otherwise when the AppDomain is recycled or the whole server goes down (you do have redundancy, right?) you’ll lose all the messages. This approach also doesn’t load balance nicely.

    Secondly, it’s just possible that you’re seeing the results of the standard collection classes not being thread-safe. Without any synchronization or other memory barriers, it’s possible that one thread isn’t seeing the data written by another. It would also be entirely possible for two threads to write to the dictionary (or write to the same list) at the same time. You could just add some synchronization, e.g. have a lock object and lock on it for the entirety of each method. I’d start using a database instead though…

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

Sidebar

Related Questions

I have created a public static class utils.cs I want to use it in
For example I now created a this tiny class: public static class FileSystemInfoComparers<T> where
I have created my own Attached Property like this: public static class LabelExtension {
I am trying to find why the class cant be created as a static?
I created a following class within a namespace Global namespace Global { public static
A college at work created a class with only static methods. Because he wants
I'm trying to access a static method in a Utils class I created: public
I need to create a static object inside a class definition. It is possible
I want to create a customized UIView class (a static lib) for some app
I intended to create a class which only have static members and static functions.

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.