473,386 Members | 1,785 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,386 software developers and data experts.

static constructor is not guaranteed to be finished?

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
Objects.

If it is true, then it is possible that some static fields are not
initialized (by static constructor) when they are used by other
methods. It is terrible.

Actually, I met some problem about static construtor. I have one
WebService with one static memeber log-writer. The log-writer is
initialized in static constructor with log file name. From time to
time, invocation of webmethod of the service will raise exception.
According to the prints of the exception, it is originated from static
constructor. It says that the log file is already opened by another
process and a Win32 IO Error is generated. That is absurd, since the
log file is only supposed to be used by the webservice.

This issue cannot be re-produced all the time. If Jesse Liberty is
right, it may explain something. The static construtor is *invoked*
before the WebService class is used. However, the execuation hangs for
some reason. The webservice run to work. some time later, the static
constructor execution resumes and bump into opening the log file. Oops,
conflict!

Just my guess. Is there anyone else meet similar situation?

-Morgan

Sep 24 '06 #1
7 2114
Are you closing the log file after writing to it? It doesn't sound like it.
If you're getting an IO exception when you initialize your log writer class,
you are attempting to at least open the file. It almost sounds like your log
writer class is keeping the file opened for its entire lifetime. That is a
*very* bad thing to do. Any unhandled exception will cause the process to
terminate, leaving the file opened. Any other form of abnormal program
termination (such as a power failure) will have the same result.When writing
to files, the rule of thumb is, open, write, and close as quickly as
possible.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

A watched clock never boils.

"Morgan Cheng" <mo************@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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
Objects.

If it is true, then it is possible that some static fields are not
initialized (by static constructor) when they are used by other
methods. It is terrible.

Actually, I met some problem about static construtor. I have one
WebService with one static memeber log-writer. The log-writer is
initialized in static constructor with log file name. From time to
time, invocation of webmethod of the service will raise exception.
According to the prints of the exception, it is originated from static
constructor. It says that the log file is already opened by another
process and a Win32 IO Error is generated. That is absurd, since the
log file is only supposed to be used by the webservice.

This issue cannot be re-produced all the time. If Jesse Liberty is
right, it may explain something. The static construtor is *invoked*
before the WebService class is used. However, the execuation hangs for
some reason. The webservice run to work. some time later, the static
constructor execution resumes and bump into opening the log file. Oops,
conflict!

Just my guess. Is there anyone else meet similar situation?

-Morgan

Sep 24 '06 #2
Hello Morgan,

See Jon's explanations there http://www.yoda.arachsys.com/csharp/...fieldinit.html

MCIn the book *Programming C#* 4th editionby Jesse Liberty, it reads
MC"Actually, the CLR guarantees to start running the static
MCconstructor before anything else is done with your class. However,
MCit only guarantees to *start* running the static constructor; it
MCdoesn't actually guarantee to *finish* running it." Page 82, Chap 4
MCClasses and Objects.
MC>
MCIf it is true, then it is possible that some static fields are not
MCinitialized (by static constructor) when they are used by other
MCmethods. It is terrible.
MC>
MCActually, I met some problem about static construtor. I have one
MCWebService with one static memeber log-writer. The log-writer is
MCinitialized in static constructor with log file name. From time to
MCtime, invocation of webmethod of the service will raise exception.
MCAccording to the prints of the exception, it is originated from
MCstatic constructor. It says that the log file is already opened by
MCanother process and a Win32 IO Error is generated. That is absurd,
MCsince the log file is only supposed to be used by the webservice.
MC>
MCThis issue cannot be re-produced all the time. If Jesse Liberty is
MCright, it may explain something. The static construtor is *invoked*
MCbefore the WebService class is used. However, the execuation hangs
MCfor some reason. The webservice run to work. some time later, the
MCstatic constructor execution resumes and bump into opening the log
MCfile. Oops, conflict!
MC>
MCJust my guess. Is there anyone else meet similar situation?
MC>
MC-Morgan
MC>
---
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Sep 24 '06 #3
Hi,

From the very basic tests I have performed in the past, it is my experience
that all access to static and instance members across threads are blocked
until the static constructor of a class has completed.

I believe the following extract from clarifies this:

ECMA spec Partition II 10.5.3.1 Type initialization guarantees
3. No methods other than those called directly or indirectly from the type
initializer are able to access members of a type before its initializer
*completes* execution.

For your problem, can you clarify, is the log file opened in the static
constructor and where is it closed?

Hope this helps
--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
"Morgan Cheng" <mo************@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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
Objects.

If it is true, then it is possible that some static fields are not
initialized (by static constructor) when they are used by other
methods. It is terrible.

Actually, I met some problem about static construtor. I have one
WebService with one static memeber log-writer. The log-writer is
initialized in static constructor with log file name. From time to
time, invocation of webmethod of the service will raise exception.
According to the prints of the exception, it is originated from static
constructor. It says that the log file is already opened by another
process and a Win32 IO Error is generated. That is absurd, since the
log file is only supposed to be used by the webservice.

This issue cannot be re-produced all the time. If Jesse Liberty is
right, it may explain something. The static construtor is *invoked*
before the WebService class is used. However, the execuation hangs for
some reason. The webservice run to work. some time later, the static
constructor execution resumes and bump into opening the log file. Oops,
conflict!

Just my guess. Is there anyone else meet similar situation?

-Morgan

Sep 24 '06 #4

Kevin Spencer 写道:
Are you closing the log file after writing to it? It doesn't sound like it.
If you're getting an IO exception when you initialize your log writer class,
you are attempting to at least open the file. It almost sounds like your log
writer class is keeping the file opened for its entire lifetime. That is a
*very* bad thing to do. Any unhandled exception will cause the process to
terminate, leaving the file opened. Any other form of abnormal program
termination (such as a power failure) will have the same result.When writing
to files, the rule of thumb is, open, write, and close as quickly as
possible.
Actually, The file is opened in static constructor and not closed
explictly in code. The webservice is supposed to run all the time. So,
I just keep it open , or else it will bring performance isssue to
opne-write-close for every log line. Opened file will be closed
automatically when the process terminates, righit?

Though the IOException can not be re-produced steadily. It happens in
this situation:
I have the webservice run; then I continue to work on the code. After
fixing some bug, I copy related assembly to deployment dir. Then type
"iisreset" in command line. This is supposed to reload new assembly.
Then I invoke the webmethod with utility. It may or may not cause the
IO exception.

Is it possible former w2wp.exe instance is still alive for some time
after "iisreset"?



--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

A watched clock never boils.

"Morgan Cheng" <mo************@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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
Objects.

If it is true, then it is possible that some static fields are not
initialized (by static constructor) when they are used by other
methods. It is terrible.

Actually, I met some problem about static construtor. I have one
WebService with one static memeber log-writer. The log-writer is
initialized in static constructor with log file name. From time to
time, invocation of webmethod of the service will raise exception.
According to the prints of the exception, it is originated from static
constructor. It says that the log file is already opened by another
process and a Win32 IO Error is generated. That is absurd, since the
log file is only supposed to be used by the webservice.

This issue cannot be re-produced all the time. If Jesse Liberty is
right, it may explain something. The static construtor is *invoked*
before the WebService class is used. However, the execuation hangs for
some reason. The webservice run to work. some time later, the static
constructor execution resumes and bump into opening the log file. Oops,
conflict!

Just my guess. Is there anyone else meet similar situation?

-Morgan
Sep 25 '06 #5

Chris Taylor 写道:
Hi,

From the very basic tests I have performed in the past, it is my experience
that all access to static and instance members across threads are blocked
until the static constructor of a class has completed.

I believe the following extract from clarifies this:

ECMA spec Partition II 10.5.3.1 Type initialization guarantees
3. No methods other than those called directly or indirectly from the type
initializer are able to access members of a type before its initializer
*completes* execution.

For your problem, can you clarify, is the log file opened in the static
constructor and where is it closed?
Since the webservice is invoked frequently. I just let the log file
opened in static constructor and don't close it. I suppose the log file
will be closed when IIS is reset.

>
Hope this helps
--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
"Morgan Cheng" <mo************@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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
Objects.

If it is true, then it is possible that some static fields are not
initialized (by static constructor) when they are used by other
methods. It is terrible.

Actually, I met some problem about static construtor. I have one
WebService with one static memeber log-writer. The log-writer is
initialized in static constructor with log file name. From time to
time, invocation of webmethod of the service will raise exception.
According to the prints of the exception, it is originated from static
constructor. It says that the log file is already opened by another
process and a Win32 IO Error is generated. That is absurd, since the
log file is only supposed to be used by the webservice.

This issue cannot be re-produced all the time. If Jesse Liberty is
right, it may explain something. The static construtor is *invoked*
before the WebService class is used. However, the execuation hangs for
some reason. The webservice run to work. some time later, the static
constructor execution resumes and bump into opening the log file. Oops,
conflict!

Just my guess. Is there anyone else meet similar situation?

-Morgan
Sep 25 '06 #6
Use the event log.

"Morgan Cheng" <mo************@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...

Kevin Spencer ??:
Are you closing the log file after writing to it? It doesn't sound like
it.
If you're getting an IO exception when you initialize your log writer
class,
you are attempting to at least open the file. It almost sounds like your
log
writer class is keeping the file opened for its entire lifetime. That is a
*very* bad thing to do. Any unhandled exception will cause the process to
terminate, leaving the file opened. Any other form of abnormal program
termination (such as a power failure) will have the same result.When
writing
to files, the rule of thumb is, open, write, and close as quickly as
possible.
Actually, The file is opened in static constructor and not closed
explictly in code. The webservice is supposed to run all the time. So,
I just keep it open , or else it will bring performance isssue to
opne-write-close for every log line. Opened file will be closed
automatically when the process terminates, righit?

Though the IOException can not be re-produced steadily. It happens in
this situation:
I have the webservice run; then I continue to work on the code. After
fixing some bug, I copy related assembly to deployment dir. Then type
"iisreset" in command line. This is supposed to reload new assembly.
Then I invoke the webmethod with utility. It may or may not cause the
IO exception.

Is it possible former w2wp.exe instance is still alive for some time
after "iisreset"?



--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

A watched clock never boils.

"Morgan Cheng" <mo************@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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
Objects.

If it is true, then it is possible that some static fields are not
initialized (by static constructor) when they are used by other
methods. It is terrible.

Actually, I met some problem about static construtor. I have one
WebService with one static memeber log-writer. The log-writer is
initialized in static constructor with log file name. From time to
time, invocation of webmethod of the service will raise exception.
According to the prints of the exception, it is originated from static
constructor. It says that the log file is already opened by another
process and a Win32 IO Error is generated. That is absurd, since the
log file is only supposed to be used by the webservice.

This issue cannot be re-produced all the time. If Jesse Liberty is
right, it may explain something. The static construtor is *invoked*
before the WebService class is used. However, the execuation hangs for
some reason. The webservice run to work. some time later, the static
constructor execution resumes and bump into opening the log file. Oops,
conflict!

Just my guess. Is there anyone else meet similar situation?

-Morgan

Sep 25 '06 #7

"Morgan Cheng" <mo************@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...

Chris Taylor ?T"s
Hi,

From the very basic tests I have performed in the past, it is my
experience
that all access to static and instance members across threads are blocked
until the static constructor of a class has completed.

I believe the following extract from clarifies this:

ECMA spec Partition II 10.5.3.1 Type initialization guarantees
3. No methods other than those called directly or indirectly from the type
initializer are able to access members of a type before its initializer
*completes* execution.

For your problem, can you clarify, is the log file opened in the static
constructor and where is it closed?
Since the webservice is invoked frequently. I just let the log file
opened in static constructor and don't close it. I suppose the log file
will be closed when IIS is reset.

Yep, when issuing an iisreset, the Worker process get's stopped, and all
process owned handles get closed.
You must have another process using the file at the same time, are you sure
no other process is reading from the logfile?.

Willy.

Sep 25 '06 #8

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

Similar topics

6
by: Marek | last post by:
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...
1
by: Peter Rilling | last post by:
Here is a weird behavior that I would not have expected in VS.NET during debugging. Maybe someone has some insight as to why this is happening. 1) I have a VS.NET web project. This contains a...
10
by: John A Grandy | last post by:
Say I have Class1 which contains static Class2 var1 = new Class2(); Is Class2 constructor code only executed if var1 is referenced in the code-execution path ? Or is Class2 constructor code...
3
by: David Veeneman | last post by:
Why do these questions always come up on Friday afternoon? I'm starting to use GoF singleton classes in my projects. Right off the bat, I've run into a surprise. I thought that a Singleton could...
7
by: James Crosswell | last post by:
I want to create a class with a static property as follows: class MyClass { private static List<MyHandlerRegisteredHandlers = new List<MyHandler>; } I then want to be able to create...
9
by: Allen | last post by:
In a static library, there is a static variable definition. static CLogger::mapFile; In both EXE and DLL, I use the same code. CLogReader uses CLogger::mapFile to do some work. CLogReader...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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,...
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
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,...

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.