473,395 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Static methods in ASP.Net

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
Nov 18 '05 #1
5 1945
It's not necessary to lock static methods.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"blah, blah, blah" <d@someone.com> wrote in message
news:Xn*********************@167.206.3.3...
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

Nov 18 '05 #2
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


Nov 18 '05 #3
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:7k********************************@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

Nov 18 '05 #4
Scott,

I assumed the warnings about threading synchronization when using the
static keyword automatically extended to methods as well as variables and
objects. Now it is clear(er) to me how things work. The variables and
parameters used by a method are thread safe unless those variables are
marked as static. Only then do you need to wrap the relevant code in a
lock statement.

Thanks for the explination.
Dan

Scott Allen <bitmask@[nospam].fred.net> wrote in
news:7k********************************@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

Nov 18 '05 #5
Another approach is to pass immutable data objects or structures to the
static method.

Regards,
Jeff
Are there better methods for preventing synchronization problems than

using the lock statement?<
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Axehelm | last post by:
Okay, I'm in a debate over whether or not static methods are a good idea in a general domain class. I'm personally not a fan of static methods but we seem to be using them to load an object. ...
4
by: Neil Zanella | last post by:
Hello, I would like to know whether it is possible to define static class methods and data members in Python (similar to the way it can be done in C++ or Java). These do not seem to be mentioned...
3
by: Steven D'Aprano | last post by:
I've been doing a lot of reading about static methods in Python, and I'm not exactly sure what they are useful for or why they were introduced. Here is a typical description of them, this one...
1
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being...
8
by: Steven Livingstone | last post by:
Anyone able to explain to me why you cannot define an interface that can then be implemented using static methods? I understand the C# CLS states this, but just interested in the reasons behind...
3
by: Mauzi | last post by:
hi, this may sound odd and noob like, but what is the 'big' difference between static and non-static funcitons ? is there any performace differnce? what is the best way to use them ? thnx ...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
8
by: Fernando Lopes | last post by:
Hi there! Someone has some code sample about when is recommend use a statis method? I know this methos don't want to be initialized and all but I want to know when I need to use it. Tks....
12
by: chandu | last post by:
hello, i want to know usage of static methods in a class. is it advantageous or disadvantage to use more static methods in a class. thank u
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.