Re: Static methods in ASP.Net
From: Kevin Spencer (kspencer_at_takempis.com)
Date: 06/21/04
- Next message: Big E: "Re: Listbox multiple insert"
- Previous message: Andrew J. Kelly: "Re: Tracking High Volume of Impressions"
- In reply to: Scott Allen: "Re: Static methods in ASP.Net"
- Next in thread: blah, blah, blah: "Re: Static methods in ASP.Net"
- Messages sorted by: [ date ] [ thread ]
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
>
- Next message: Big E: "Re: Listbox multiple insert"
- Previous message: Andrew J. Kelly: "Re: Tracking High Volume of Impressions"
- In reply to: Scott Allen: "Re: Static methods in ASP.Net"
- Next in thread: blah, blah, blah: "Re: Static methods in ASP.Net"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|