Re: Static methods in ASP.Net

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Kevin Spencer (kspencer_at_takempis.com)
Date: 06/21/04


Date: Mon, 21 Jun 2004 13:54:17 -0400

Well, Scott, you're not really disagreeing with me, but clarifying my
remarks. You are correct, in that, if the static method accesses and
modifies external static data, yes, that data would have to be locked. I
just didn't point that out. Thanks for the clarification.

-- 
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:7ksdd0ti8cgpq6s115c50vt27q4a7m8b04@4ax.com...
> It's hard to say yes or no without knowing exactly what you are doing.
> If you posted some code that would be helpful.
>
> I'd disagree with Kevin and say there are certainly static method
> scenarios where  you need locks to keep the application well behaved.
>
> If each thread operates on data specific to the thread, then no locks
> are needed. For example:
>
> public static string GetCookieName(Cookie c)
> {
>     return c.Name;
> }
>
> If, on the other hand, multiple threads can access a shared object,
> you need to be very careful:
>
> static ArrayList cookieList = new ArrayList();
>
> public static void AddToCookieList(Cookie c)
> {
>     cookieList.Add(c);
> }
>
> No matter how fast the above method looks, the chance of corruption
> exists because it doesn't prevent multiple threads from getting inside
> the Add method.
>
> --
> Scott
> http://www.OdeToCode.com
>
> On Mon, 21 Jun 2004 12:23:55 GMT, "blah, blah, blah" <d@someone.com>
> wrote:
>
> >I'm developing a .Net web application and created many helper classes
> >often using static (shared in VB.Net) methods. Do I need to use the lock
> >(SyncLock) statement in these methods to prevent multiple web application
> >threads from using the same method at the same time?
> >
> >For example, I have a static method that extracts information from a page
> >request cookie for use by other parts of the application. Since this
> >happens on every request I thought it would be a waste of resources to
> >create a new object just to provide the cookie method, so I made it
> >static. But then I thought if two different requests come into the
> >application milliseconds apart and two different threads request the same
> >static cookie method it could cause problems. The method is fast so it is
> >unlikely multiple threads will have a synchronization problem, but it
> >could happen (right?) and I do have other static methods take longer to
> >execute.
> >
> >I'm not spawing a new thread to do any asynchronous processing, but it is
> >my understanding that IIS and ASP.Net are inherently multi-threaded when
> >dealing with page requests from different browsers/computers.
> >
> >I've googled and searched all over the place but I can't find a clear
> >answer on if, when or how to use a lock statement in shared methods
> >specifically with web applications.
> >
> >What are the general guidelines for using shared methods in web
> >applications?
> >
> >Are there better methods for preventing synchronization problems than
> >using the lock statement?
> >
> >Any pointers or tips are appreciated.
> >Dan
>


Relevant Pages

  • Re: Static methods in ASP.Net
    ... Do I need to use the lock ... >request cookie for use by other parts of the application. ... >static cookie method it could cause problems. ... when or how to use a lock statement in shared methods ...
    (microsoft.public.dotnet.framework.aspnet)
  • Static methods in ASP.Net
    ... Do I need to use the lock ... request cookie for use by other parts of the application. ... static cookie method it could cause problems. ... unlikely multiple threads will have a synchronization problem, ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: multiple instance on Unix
    ... I know the approach to creating a lock file has been around a long time ... The subsequent instances could try to create a directory ... When you're using the filesystem as either a locking ... The first request would be handled and processed, ...
    (comp.lang.python)
  • Re: multiple instance on Unix
    ... I know the approach to creating a lock file has been around a long time ... The subsequent instances could try to create a directory ... When you're using the filesystem as either a locking ... The first request would be handled and processed, ...
    (comp.lang.python)
  • Re: [PATCH 0/7] dlm: overview
    ... >> lock requests involve sending a message to a remote machine and waiting ... >> a request, which is why I'm a bit confused by your question. ... how does oracle's dlm perform for your questions above? ... >> I'd suggest that if some cluster application is using the dlm and has poor ...
    (Linux-Kernel)