473,395 Members | 1,972 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 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.SystemFramework {

(some code...)

public class ApplicationLog {
(some code..)
static ApplicationLog() {
Type myType = typeof(ApplicationLog);
try {
if (!Monitor.TryEnter(myType)) {
Monitor.Enter(myType);
return;
}
(some code..)
}
finally {
Monitor.Exit(myType);
}

} // static constructor

} // class ApplicationLog

(some code..)

} //namespace Duwamish7.SystemFramework
namespace Duwamish7.SystemFramework {

(...)

public class ApplicationLog {

Type myType = typeof(ApplicationLog);
try {

if (!Monitor.TryEnter(myType)) {

Monitor.Enter(myType);

return;

}

(...)

Thank you in advance.

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

namespace Duwamish7.SystemFramework {

(some code...)

public class ApplicationLog {
(some code..)
static ApplicationLog() {
Type myType = typeof(ApplicationLog);
try {
if (!Monitor.TryEnter(myType)) {
Monitor.Enter(myType);
return;
}
(some code..)
}
finally {
Monitor.Exit(myType);
}

} // static constructor

} // class ApplicationLog

(some code..)

} //namespace Duwamish7.SystemFramework
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.Locking 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.net> 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
SystemFramework within module ApplicationLog.cs. Here comes the sample.

namespace Duwamish7.SystemFramework {

(some code...)

public class ApplicationLog {
(some code..)
static ApplicationLog() {
Type myType = typeof(ApplicationLog);
try {
if (!Monitor.TryEnter(myType)) {
Monitor.Enter(myType);
return;
}
(some code..)
}
finally {
Monitor.Exit(myType);
}

} // static constructor

} // class ApplicationLog

(some code..)

} //namespace Duwamish7.SystemFramework
namespace Duwamish7.SystemFramework {

(...)

public class ApplicationLog {

Type myType = typeof(ApplicationLog);
try {

if (!Monitor.TryEnter(myType)) {

Monitor.Enter(myType);

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.com...
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.Locking 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.net> 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
SystemFramework within module ApplicationLog.cs. Here comes the sample.

namespace Duwamish7.SystemFramework {

(some code...)

public class ApplicationLog {
(some code..)
static ApplicationLog() {
Type myType = typeof(ApplicationLog);
try {
if (!Monitor.TryEnter(myType)) {
Monitor.Enter(myType);
return;
}
(some code..)
}
finally {
Monitor.Exit(myType);
}

} // static constructor

} // class ApplicationLog

(some code..)

} //namespace Duwamish7.SystemFramework
namespace Duwamish7.SystemFramework {

(...)

public class ApplicationLog {

Type myType = typeof(ApplicationLog);
try {

if (!Monitor.TryEnter(myType)) {

Monitor.Enter(myType);

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(ApplicationLog). 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.net> 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
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
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(ApplicationLog) 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.com...
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(ApplicationLog). 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.net> 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
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
http://www.OdeToCode.com

Nov 18 '05 #6
OK. I understand now.
Other class' methods can also put a lock on typeof(ApplicationLog) 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.com...
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(ApplicationLog). 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.net> 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
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
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
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...
3
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...
11
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...
5
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...
12
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...
7
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...
8
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....
6
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...
5
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...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
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,...

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.