locked
Lock static variable? RRS feed

  • Question

  • User-1221430945 posted

    I read this guide of how you can keep track of onlineusers
    http://www.aspdotnetfaq.com/Faq/How-to-get-detailed-info-about-all-online-visitors-that-are-currently-browsing-your-ASP-NET-website.aspx

    My question is:
    Do we need to lock the property "OnlineVisitorsContainer.Visitors", which is static and shared between all users?

    void Session_Start(object sender, EventArgse)

        {

            // add some data to the Session so permanent SessionId is assigned

            // otherwise new SessionId is assigned to the user until some data

            // is actually written to Session

            Session["Start"] = DateTime.Now;

            // get current context

            HttpContext currentContext = HttpContext.Current;

            if (currentContext != null)

            {

                if (!OnlineVisitorsContainer.Visitors.ContainsKey(currentContext.Session.SessionID))

                {

                    WebsiteVisitor currentVisitor = new WebsiteVisitor(currentContext);              

                    OnlineVisitorsContainer.Visitors.Add(currentVisitor.SessionId, currentVisitor);

                }

            }

        }

    Sunday, February 12, 2012 6:05 AM

Answers

  • User612574037 posted

    Hi,

    I think you’d better lock it. If you don’t lock it you may come across some problems with a lot of users. One user may operate the current OnlineVisitorsContainer.Visitors but the OnlineVisitorsContainer.Visitors has been changed by other users just after it. As the user is still use the old OnlineVisitorsContainer.Visitors it doesn’t know the Visitors proerty has changed. So you should lock the Visitors when one user is operate it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 14, 2012 2:51 AM

All replies

  • User1109032460 posted

    Yes.

    Sunday, February 12, 2012 2:28 PM
  • User612574037 posted

    Hi,

    I think you’d better lock it. If you don’t lock it you may come across some problems with a lot of users. One user may operate the current OnlineVisitorsContainer.Visitors but the OnlineVisitorsContainer.Visitors has been changed by other users just after it. As the user is still use the old OnlineVisitorsContainer.Visitors it doesn’t know the Visitors proerty has changed. So you should lock the Visitors when one user is operate it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 14, 2012 2:51 AM
  • User-1221430945 posted

    Thanx, I guess you are right so I think I will lock using this code..

    lock (OnlineVisitors.Visitors)
    {
        OnlineVisitors.Visitors.add(...);
    }

    However, I made a test and started 5000 threads and at the end OnlineVisitors.Visitors contained 5000 items (without locking)..so maybe we dont need a lock statement if we are just adding new items to OnlineVisitors.Visitors (which is a static dictionary in this case)?..

    I guess it must be confusing when an item should be added in the dictionary but someone (another thread) just took that memory before you..But it seems that its smart enough to just pick the memory after that, right?..The order is not important for me in this case, what I care about is that all items will be added.

    Wednesday, February 15, 2012 3:59 PM
  • User1109032460 posted

    No.

    You got lucky.

    You need to lock. The standard collections in .NET are NOT thread-safe (this is by design, due to the additional costs of performing locking which are not required in many scenarios). Therefore, if you intend to manipulate it from multiple threads simulataneously, you must add your own synchronisation code.

    Monday, February 20, 2012 12:27 PM
  • User-1666726364 posted

    Dear All,

    I am really need help, kindly advice the solution on my issue.

    I am also using this guide to do counting visitors :http://www.aspdotnetfaq.com/Faq/How-to-get-detailed-info-about-all-online-visitors-that-are-currently-browsing-your-ASP-NET-website.aspx

    I am now facing a problem, some time a error message appear with error message is as: 

    Server Error in '/' Application.


    Index was outside the bounds of the array.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.IndexOutOfRangeException: Index was outside the bounds of the array.

    Source Error: 

    Line 46:                 lock (OnlineVisitorsContainer.Visitors)
    Line 47:                 {
    Line 48: OnlineVisitorsContainer.Visitors.Add(currentVisitor.SessionId, currentVisitor); Line 49:                 }
    Line 50:             }


    Source File: d:\vhosts\xxx.xxx\httpdocs\Global.asax    Line: 48 

    Stack Trace: 

    [IndexOutOfRangeException: Index was outside the bounds of the array.]
       System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +9378005
       System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value) +11
       ASP.global_asax.Session_Start(Object sender, EventArgs e) in d:\vhosts\xxx.xxx\httpdocs\Global.asax:48
       System.Web.SessionState.SessionStateModule.RaiseOnStart(EventArgs e) +9028291
       System.Web.SessionState.SessionStateModule.CompleteAcquireState() +148
       System.Web.SessionState.SessionStateModule.BeginAcquireState(Object source, EventArgs e, AsyncCallback cb, Object extraData) +561
       System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +96
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

    I have tried to use this, but doens't help

    Line 46:                 lock (OnlineVisitorsContainer.Visitors)
    Line 47:                 {
    Line 48: OnlineVisitorsContainer.Visitors.Add(currentVisitor.SessionId, currentVisitor); 
    Line 49:                 }

    Kindly advice. 

    Thank you.

    Wednesday, February 6, 2013 5:06 AM