473,671 Members | 2,504 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 ReaderWriterLoc k 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.Co unt. 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.Co unt.

Will the second thread: (1) tries to invoke the constructor again, (2) waits till the first thread is finished before it enters MyCollection.Co unt, (3) gets access to MyCollection.Co unt 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.Cou nt
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.Threadin g.ReaderWriterL ock
Public Shared Function Count() as Integer

Lock.AcquireRea derLock(Timeout .Infinite)

Count = mCollection.Cou nt
Lock.ReleaseRea derLock()
End Function

Shared Sub New
Try

Lock.AcquireWri terLock(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.ReleaseWri terLock()
End Try

End Sub

End Class

Nov 19 '05 #1
2 1194
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)...ot herwise 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**********@g mail.com> wrote in message
news:uU******** *****@TK2MSFTNG P12.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 ReaderWriterLoc k 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.Co unt. 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.Co unt.

Will the second thread: (1) tries to invoke the constructor again, (2) waits
till the first thread is finished before it enters MyCollection.Co unt, (3)
gets access to MyCollection.Co unt 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.Cou nt
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.Threadin g.ReaderWriterL ock

Public Shared Function Count() as Integer
Lock.AcquireRea derLock(Timeout .Infinite)
Count = mCollection.Cou nt
Lock.ReleaseRea derLock()
End Function

Shared Sub New

Try
Lock.AcquireWri terLock(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.ReleaseWri terLock()
End Try
End Sub
End Class
Nov 19 '05 #2
Thanks Karl:

The article really helped.

Paul Wu

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:Op******** ******@TK2MSFTN GP14.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)...ot herwise 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**********@g mail.com> wrote in message
news:uU******** *****@TK2MSFTNG P12.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 ReaderWriterLoc k 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.Co unt. 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.Co unt.

Will the second thread: (1) tries to invoke the constructor again, (2)
waits till the first thread is finished before it enters
MyCollection.Co unt, (3) gets access to MyCollection.Co unt 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.Cou nt
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.Threadin g.ReaderWriterL ock

Public Shared Function Count() as Integer
Lock.AcquireRea derLock(Timeout .Infinite)
Count = mCollection.Cou nt
Lock.ReleaseRea derLock()
End Function

Shared Sub New

Try
Lock.AcquireWri terLock(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.ReleaseWri terLock()
End Try
End Sub
End Class

Nov 19 '05 #3

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

Similar topics

10
3557
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 class *Shared* methods which make use of the class ID. So I gave the base class a classID field, and then I gave the derived classes Shared constructors, which I used to set the classID field to the appropriate values for each derived class. But...
2
3739
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 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...
10
3673
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' variable for the application. Any other code within the application can access it. public - can be shared across the application if instatiated. Does that sound about right? It seems these are more useful for methods rather than variables. Most of...
15
4927
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 String ' this is ineffect a global application object End Module
5
11591
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 explicitly create a shared class, how does vb.net creates it? TIA, Erik Cruz
0
8483
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8402
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8927
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8825
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8676
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5703
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4416
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.