Say you want to share some resource, like a class or a variable across all threads/sessions within a ASP.NET web application. What is better?
1) A static variable having thread-safe accessors to that static variable?
2) Or a ASP.NET application session variable?
If you only have one of those, there is little difference.
If you have several, you should use static variables rather than Application
variables. The Application.Lock
method will lock all Application
variables, while you can use separate syncronisition identifiers for your static variables, so that each lock only affects the code that accesses that specific variable.
Static Members will offer better performance, but the down-side is they are not thread safe:
It is recommended that you store data in static members of the application class instead of in the Application object. This increases performance because you can access a static variable faster than you can access an item in the Application dictionary.
From: http://support.microsoft.com/default.aspx?scid=kb;en-us;Q312607
and:
http://weblogs.asp.net/plip/archive/2003/12/01/40526.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With