473,511 Members | 16,769 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

clarification on Shared class constructor

From what I understand, in ASP.NET, each HTTP requests is serviced by a separate thread. So if my code uses a static Class with shared members and properties, I can manage concurrent access by using something like the Monitor or ReaderWriterLock class. It is rather difficult to simulate multiple threads in a debug environment so I am hoping someone can enlighten me by telling me what happens in the following scenario?

A request is made to the IIS which in turn launches a thread to access a shared member MyCollection.Count. Since the class has not been initialized, the Constructor is invoked and initializes class with some data from the database. Before the first thread exits the constructor, a second request is made to the IIS which spawns a second thread to call MyCollection.Count.

Will the second thread: (1) tries to invoke the constructor again, (2) waits till the first thread is finished before it enters MyCollection.Count, (3) gets access to MyCollection.Count and get a zero (or a number less than the actual count) because the collection is not fully initialized yet.

Public Class MyCollection
Private Shared mCollection as New Hashtable



Public Shared Function Count() as Integer
Return mCollection.Count
End Function

Shared Sub New
'code to initialize a datatable
For each record in datatable
Dim obj as New Employee(record.column("name"))
mCollection.Add(key, obj)
Next
End Sub

End Class

Should I be doing something like the following:

Public Classs MyCollection
Private Shared mCollection as New Hashtable

Private Shared Lock As New System.Threading.ReaderWriterLock
Public Shared Function Count() as Integer

Lock.AcquireReaderLock(Timeout.Infinite)

Count = mCollection.Count
Lock.ReleaseReaderLock()
End Function

Shared Sub New
Try

Lock.AcquireWriterLock(Timeout.Infinite)

'code to initialize a datatable

...
For each record in datatable
Dim obj as New Employee(record.column("name"))
mCollection.Add(key, obj)
Next
Finally

Lock.ReleaseWriterLock()
End Try

End Sub

End Class

Nov 17 '05 #1
2 3731
Paul:
Static constructors are thread-safe, no if or buts about it. That means,
from a static constructor point of view, you are assured that...the only
thing you need to worry about is if your static constructor relies on a 2nd
static constructor which has a reference back to the 1st class (cyclical
reference)...otherwise your safe.

As for as your properties, they aren't thread-safe...unless the underlying
class itself is (some .net classes are thread safe).

You should take a look at:
http://odetocode.com/Articles/313.aspx
and
http://odetocode.com/Articles/314.aspx

As you hopefully know, locks come with a hefty performance price and you
should consider alternatives if possible...I haven't had the need to use
static fields too often...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Paul Wu" <wu**********@gmail.com> wrote in message
news:uU*************@TK2MSFTNGP12.phx.gbl...
From what I understand, in ASP.NET, each HTTP requests is serviced by a
separate thread. So if my code uses a static Class with shared members and
properties, I can manage concurrent access by using something like the
Monitor or ReaderWriterLock class. It is rather difficult to simulate
multiple threads in a debug environment so I am hoping someone can enlighten
me by telling me what happens in the following scenario?

A request is made to the IIS which in turn launches a thread to access a
shared member MyCollection.Count. Since the class has not been initialized,
the Constructor is invoked and initializes class with some data from the
database. Before the first thread exits the constructor, a second request is
made to the IIS which spawns a second thread to call MyCollection.Count.

Will the second thread: (1) tries to invoke the constructor again, (2) waits
till the first thread is finished before it enters MyCollection.Count, (3)
gets access to MyCollection.Count and get a zero (or a number less than the
actual count) because the collection is not fully initialized yet.
Public Class MyCollection
Private Shared mCollection as New Hashtable

Public Shared Function Count() as Integer
Return mCollection.Count
End Function

Shared Sub New
'code to initialize a datatable
For each record in datatable
Dim obj as New Employee(record.column("name"))
mCollection.Add(key, obj)
Next
End Sub
End Class

Should I be doing something like the following:
Public Classs MyCollection
Private Shared mCollection as New Hashtable
Private Shared Lock As New System.Threading.ReaderWriterLock

Public Shared Function Count() as Integer
Lock.AcquireReaderLock(Timeout.Infinite)
Count = mCollection.Count
Lock.ReleaseReaderLock()
End Function

Shared Sub New

Try
Lock.AcquireWriterLock(Timeout.Infinite)
'code to initialize a datatable
...
For each record in datatable
Dim obj as New Employee(record.column("name"))
mCollection.Add(key, obj)
Next
Finally
Lock.ReleaseWriterLock()
End Try
End Sub
End Class
Nov 17 '05 #2
Thanks Karl:

The article really helped.

Paul Wu

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:Op**************@TK2MSFTNGP14.phx.gbl...
Paul:
Static constructors are thread-safe, no if or buts about it. That means,
from a static constructor point of view, you are assured that...the only
thing you need to worry about is if your static constructor relies on a
2nd static constructor which has a reference back to the 1st class
(cyclical reference)...otherwise your safe.

As for as your properties, they aren't thread-safe...unless the underlying
class itself is (some .net classes are thread safe).

You should take a look at:
http://odetocode.com/Articles/313.aspx
and
http://odetocode.com/Articles/314.aspx

As you hopefully know, locks come with a hefty performance price and you
should consider alternatives if possible...I haven't had the need to use
static fields too often...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Paul Wu" <wu**********@gmail.com> wrote in message
news:uU*************@TK2MSFTNGP12.phx.gbl...
From what I understand, in ASP.NET, each HTTP requests is serviced by a
separate thread. So if my code uses a static Class with shared members and
properties, I can manage concurrent access by using something like the
Monitor or ReaderWriterLock class. It is rather difficult to simulate
multiple threads in a debug environment so I am hoping someone can
enlighten me by telling me what happens in the following scenario?

A request is made to the IIS which in turn launches a thread to access a
shared member MyCollection.Count. Since the class has not been
initialized, the Constructor is invoked and initializes class with some
data from the database. Before the first thread exits the constructor, a
second request is made to the IIS which spawns a second thread to call
MyCollection.Count.

Will the second thread: (1) tries to invoke the constructor again, (2)
waits till the first thread is finished before it enters
MyCollection.Count, (3) gets access to MyCollection.Count and get a zero
(or a number less than the actual count) because the collection is not
fully initialized yet.
Public Class MyCollection
Private Shared mCollection as New Hashtable

Public Shared Function Count() as Integer
Return mCollection.Count
End Function

Shared Sub New
'code to initialize a datatable
For each record in datatable
Dim obj as New Employee(record.column("name"))
mCollection.Add(key, obj)
Next
End Sub
End Class

Should I be doing something like the following:
Public Classs MyCollection
Private Shared mCollection as New Hashtable
Private Shared Lock As New System.Threading.ReaderWriterLock

Public Shared Function Count() as Integer
Lock.AcquireReaderLock(Timeout.Infinite)
Count = mCollection.Count
Lock.ReleaseReaderLock()
End Function

Shared Sub New

Try
Lock.AcquireWriterLock(Timeout.Infinite)
'code to initialize a datatable
...
For each record in datatable
Dim obj as New Employee(record.column("name"))
mCollection.Add(key, obj)
Next
Finally
Lock.ReleaseWriterLock()
End Try
End Sub
End Class

Nov 17 '05 #3

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

Similar topics

10
3524
by: John Brock | last post by:
I have a base class with several derived classes (I'm writing in VB.NET). I want each derived class to have a unique class ID (a String), and I want the derived classes to inherit from the base...
10
3662
by: darrel | last post by:
I'm still trying to sort out in my head the differences between public and shared when referring to declaring properties or variables. This is my understanding: shared - akin to a 'global'...
2
1186
by: Paul Wu | last post by:
From what I understand, in ASP.NET, each HTTP requests is serviced by a separate thread. So if my code uses a static Class with shared members and properties, I can manage concurrent access by using...
15
4898
by: Rob Nicholson | last post by:
A consequence of the ASP.NET architecture on IIS has just hit home with a big thud. It's to do with shared variables. Consider a module like this: Public Module Functions Public GlobalName As...
5
11570
by: Erik Cruz | last post by:
Hello! I have read some threads discussing the fact that a module is in reality a shared class. If I try to create a Public Shared Class in vb.net I receive a compile error. Why? If I can't...
0
7355
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7423
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7081
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
7510
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...
1
5066
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1576
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
447
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.