473,587 Members | 2,324 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static constructor

Hi,

I am analyzing Duwamish7 source code boundled with Visual Studio .NET 2003.
Could anoybody explain why the Monitor.Enter and Monitor.Exit block is used
inside a static constructor? The code can be found in Project
SystemFramework within module ApplicationLog. cs. Here comes the sample.

namespace Duwamish7.Syste mFramework {

(some code...)

public class ApplicationLog {
(some code..)
static ApplicationLog( ) {
Type myType = typeof(Applicat ionLog);
try {
if (!Monitor.TryEn ter(myType)) {
Monitor.Enter(m yType);
return;
}
(some code..)
}
finally {
Monitor.Exit(my Type);
}

} // static constructor

} // class ApplicationLog

(some code..)

} //namespace Duwamish7.Syste mFramework
namespace Duwamish7.Syste mFramework {

(...)

public class ApplicationLog {

Type myType = typeof(Applicat ionLog);
try {

if (!Monitor.TryEn ter(myType)) {

Monitor.Enter(m yType);

return;

}

(...)

Thank you in advance.

Mark
Nov 18 '05 #1
6 1435
The code in my post should look like this:

namespace Duwamish7.Syste mFramework {

(some code...)

public class ApplicationLog {
(some code..)
static ApplicationLog( ) {
Type myType = typeof(Applicat ionLog);
try {
if (!Monitor.TryEn ter(myType)) {
Monitor.Enter(m yType);
return;
}
(some code..)
}
finally {
Monitor.Exit(my Type);
}

} // static constructor

} // class ApplicationLog

(some code..)

} //namespace Duwamish7.Syste mFramework
Nov 18 '05 #2
I don't think this is a very good example to follow, Markek. I don't
see why a Monitor is useful here at all. Also, it's a bad idea to lock
on a type object, Microsoft has been attempting to scrub this practice
out of the examples.Lockin g on a type is a very coarse lock on an
object that is expensive to create, and anyone inside the app domain
(even out) can aquire the type object and take a lock, which opens up
the potential for deadlock.

--
Scott
http://www.OdeToCode.com

On Fri, 07 May 2004 06:15:23 GMT, "Marek"
<ma************ **@sbcglobal.ne t> wrote:
Hi,

I am analyzing Duwamish7 source code boundled with Visual Studio .NET 2003.
Could anoybody explain why the Monitor.Enter and Monitor.Exit block is used
inside a static constructor? The code can be found in Project
SystemFramewor k within module ApplicationLog. cs. Here comes the sample.

namespace Duwamish7.Syste mFramework {

(some code...)

public class ApplicationLog {
(some code..)
static ApplicationLog( ) {
Type myType = typeof(Applicat ionLog);
try {
if (!Monitor.TryEn ter(myType)) {
Monitor.Enter(m yType);
return;
}
(some code..)
}
finally {
Monitor.Exit(my Type);
}

} // static constructor

} // class ApplicationLog

(some code..)

} //namespace Duwamish7.Syste mFramework
namespace Duwamish7.Syste mFramework {

(...)

public class ApplicationLog {

Type myType = typeof(Applicat ionLog);
try {

if (!Monitor.TryEn ter(myType)) {

Monitor.Enter(m yType);

return;

}

(...)

Thank you in advance.

Mark


Nov 18 '05 #3
Hi Scott,

Thank you for your explanation but there's still something that I don't
understand.
Please listen to the following and point where I make a mistake.

1. Static constructor (SC) is guaranteed to be executed not more then once
(isn't it?).
2. If a class contains only static methods (which is the case) SC is
executed (implicite) BEFORE the first call to a static method.
3. That means that SC code cannot interfere (i.e. be executed
simultaneously) with any other static method code.
4. As a result, there's no need to protect execution of SC against
multithreading safety.

Helper question. Is it possible the a static method (let's say in another
thread) starts its execution before the execution of static constructor
ends?

Thanks
Marek

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:dq******** *************** *********@4ax.c om...
I don't think this is a very good example to follow, Markek. I don't
see why a Monitor is useful here at all. Also, it's a bad idea to lock
on a type object, Microsoft has been attempting to scrub this practice
out of the examples.Lockin g on a type is a very coarse lock on an
object that is expensive to create, and anyone inside the app domain
(even out) can aquire the type object and take a lock, which opens up
the potential for deadlock.

--
Scott
http://www.OdeToCode.com

On Fri, 07 May 2004 06:15:23 GMT, "Marek"
<ma************ **@sbcglobal.ne t> wrote:
Hi,

I am analyzing Duwamish7 source code boundled with Visual Studio .NET 2003.Could anoybody explain why the Monitor.Enter and Monitor.Exit block is usedinside a static constructor? The code can be found in Project
SystemFramewor k within module ApplicationLog. cs. Here comes the sample.

namespace Duwamish7.Syste mFramework {

(some code...)

public class ApplicationLog {
(some code..)
static ApplicationLog( ) {
Type myType = typeof(Applicat ionLog);
try {
if (!Monitor.TryEn ter(myType)) {
Monitor.Enter(m yType);
return;
}
(some code..)
}
finally {
Monitor.Exit(my Type);
}

} // static constructor

} // class ApplicationLog

(some code..)

} //namespace Duwamish7.Syste mFramework
namespace Duwamish7.Syste mFramework {

(...)

public class ApplicationLog {

Type myType = typeof(Applicat ionLog);
try {

if (!Monitor.TryEn ter(myType)) {

Monitor.Enter(m yType);

return;

}

(...)

Thank you in advance.

Mark

Nov 18 '05 #4
Hi Marek:

The static constructor will execute only once.

If we assume the lock inside of the constructor is only for the
purpose of protecting the static constructor from concurrent access,
we could say the lock is not needed. But, we don't know where else
this lock might be used, because some other code might lock on
typeof(Applicat ionLog). Perhaps the lock is trying to protect some
other resource the static constructor touches. Thus, this code is not
a great example to look at, particularly in isolation.

--s
On Fri, 07 May 2004 18:59:11 GMT, "Marek"
<ma************ **@sbcglobal.ne t> wrote:
Hi Scott,

Thank you for your explanation but there's still something that I don't
understand.
Please listen to the following and point where I make a mistake.

1. Static constructor (SC) is guaranteed to be executed not more then once
(isn't it?).
2. If a class contains only static methods (which is the case) SC is
executed (implicite) BEFORE the first call to a static method.
3. That means that SC code cannot interfere (i.e. be executed
simultaneously ) with any other static method code.
4. As a result, there's no need to protect execution of SC against
multithreadi ng safety.

Helper question. Is it possible the a static method (let's say in another
thread) starts its execution before the execution of static constructor
ends?

Thanks
Marek


--
Scott
http://www.OdeToCode.com
Nov 18 '05 #5
Hi Scott

The only thing that I've been trying to find out is whether this code is a
piece of shit or a very advanced technique to achieve something.

I have to ask what do you mean by saying "the lock is trying to protect some
other resources the static constructor touches"?
How can putting a lock on typeof(Applicat ionLog) protect anything else but
the type class instance itself? Are you sure you didn't make mistake in what
you said?

Marek

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:r2******** *************** *********@4ax.c om...
Hi Marek:

The static constructor will execute only once.

If we assume the lock inside of the constructor is only for the
purpose of protecting the static constructor from concurrent access,
we could say the lock is not needed. But, we don't know where else
this lock might be used, because some other code might lock on
typeof(Applicat ionLog). Perhaps the lock is trying to protect some
other resource the static constructor touches. Thus, this code is not
a great example to look at, particularly in isolation.

--s
On Fri, 07 May 2004 18:59:11 GMT, "Marek"
<ma************ **@sbcglobal.ne t> wrote:
Hi Scott,

Thank you for your explanation but there's still something that I don't
understand.
Please listen to the following and point where I make a mistake.

1. Static constructor (SC) is guaranteed to be executed not more then once(isn't it?).
2. If a class contains only static methods (which is the case) SC is
executed (implicite) BEFORE the first call to a static method.
3. That means that SC code cannot interfere (i.e. be executed
simultaneously ) with any other static method code.
4. As a result, there's no need to protect execution of SC against
multithreadi ng safety.

Helper question. Is it possible the a static method (let's say in another
thread) starts its execution before the execution of static constructor
ends?

Thanks
Marek


--
Scott
http://www.OdeToCode.com

Nov 18 '05 #6
OK. I understand now.
Other class' methods can also put a lock on typeof(Applicat ionLog) before
the static constructor starts its execution.
Thus, in some cases putting a lock inside a static constructor can be
reasonable.

Thank you.
Marek

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:r2******** *************** *********@4ax.c om...
Hi Marek:

The static constructor will execute only once.

If we assume the lock inside of the constructor is only for the
purpose of protecting the static constructor from concurrent access,
we could say the lock is not needed. But, we don't know where else
this lock might be used, because some other code might lock on
typeof(Applicat ionLog). Perhaps the lock is trying to protect some
other resource the static constructor touches. Thus, this code is not
a great example to look at, particularly in isolation.

--s
On Fri, 07 May 2004 18:59:11 GMT, "Marek"
<ma************ **@sbcglobal.ne t> wrote:
Hi Scott,

Thank you for your explanation but there's still something that I don't
understand.
Please listen to the following and point where I make a mistake.

1. Static constructor (SC) is guaranteed to be executed not more then once(isn't it?).
2. If a class contains only static methods (which is the case) SC is
executed (implicite) BEFORE the first call to a static method.
3. That means that SC code cannot interfere (i.e. be executed
simultaneously ) with any other static method code.
4. As a result, there's no need to protect execution of SC against
multithreadi ng safety.

Helper question. Is it possible the a static method (let's say in another
thread) starts its execution before the execution of static constructor
ends?

Thanks
Marek


--
Scott
http://www.OdeToCode.com

Nov 18 '05 #7

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

Similar topics

5
5532
by: A.M | last post by:
Hi, I have i utility class contains static functions (and also members) I usually use in may applications. Can i have a static like constructor so any time the app starts, The constructor initializes static class members ? Thanks, Ali
3
11819
by: Kirk Marple | last post by:
Just want to see if this is 'by design' or a bug... I have a common List<T> defined in a base class, and the base class has a static property to expose this list. I wanted the derived class to add items to this list, but have the base class hold the data and expose the property. Problem is, however, that the derived class' static...
11
3814
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of...
5
3078
by: Tina | last post by:
I'm using C# 1.1.............. I add new Class item to my project. It's created as class Math I change it to public static class Math I get an error saying that "The modifier 'static' is not valid for this item. Why? Are static classes not allowed??
12
3420
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this static constructor of the base class would be invoked even if a an object of the derived class is created. Is this correct ? Is there any way to...
7
2140
by: Morgan Cheng | last post by:
In the book *Programming C#* 4th editionby Jesse Liberty, it reads "Actually, the CLR guarantees to start running the static constructor before anything else is done with your class. However, it only guarantees to *start* running the static constructor; it doesn't actually guarantee to *finish* running it." Page 82, Chap 4 Classes and...
8
8918
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it. Like, for instance the Objective-C method: +(void)initialize Which has the following characteristics: It is guaranteed to be run
6
7249
by: =?Utf-8?B?TWF0dA==?= | last post by:
I'm having a problem with a static class constructor being called twice. I have the static class MasterTaskList which uses a BackgroundWorker to execute multiple methods on a separate thread. The static constructor calls a reset function which creates a new instance of BackgroundWorker and attaches the appropriate event handlers. There is also...
5
4453
by: Dave | last post by:
Hello, Suppose you have a class with a static property with only a get (read only). You also have code in a static constructor that sets these properties but takes 1 hour to run. Now suppose the first request comes in at 11:00 am and tries to read from this property. It will need to wait an hour until the page loads which is fine and...
0
7843
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...
1
7967
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
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...
0
6621
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5392
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...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2353
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 we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.