Is this code thread safe? (question about the use of lock statement)

11 views
Skip to first unread message

Guilherme Utrabo

unread,
Feb 10, 2012, 1:26:02 PM2/10/12
to dotnetde...@googlegroups.com
Hello everybody. Need some help with the "lock" statement.
I have the following structure on my Global.asax:
 
private static IList MyList;
private static object syncObject = new object();
void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
{
      if (MyList == null)
      {
            lock (syncObject)
            {
                  MyList = new ArrayList();
                  MyList.Add("test");
            }
      }
}
 
When my application starts I want to populate "MyList" and then share it between the users.
But I want to make it once. Is my structure really thread safe? Should I use the lock on "MyList" instead of using it on the "syncObject"?
 
Thanks in advance.
 
Regards,
Guilherme

Reuben Bartolo

unread,
Feb 11, 2012, 6:42:28 AM2/11/12
to dotnetde...@googlegroups.com
What you are trying to achieve there, is called the Singleton Design Pattern.
I suggest you do some research into it, and any pitfalls it might have.

Regards,
Reuben Bartolo

--
You received this message because you are subscribed to the Google
Groups "DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML
Web Services,.NET Remoting" group.
To post to this group, send email to dotnetde...@googlegroups.com
To unsubscribe from this group, send email to
dotnetdevelopm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/dotnetdevelopment?hl=en?hl=en
or visit the group website at http://megasolutions.net

Rene Felix Correa

unread,
May 15, 2012, 7:39:14 AM5/15/12
to dotnetde...@googlegroups.com
Why you don't use the method Application_Start in the global.asax

This method only call when the app is start, and if you put in a static variable the content any user can acces this content.

Useful link's
--
Rene Felix Correa
Analista Desenvolvedor
http://renefc3.wordpress.com

Reply all
Reply to author
Forward
0 new messages