473,396 Members | 2,020 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,396 software developers and data experts.

Static Constructor in ASP.NET app

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 makes sense. If a second (different) request at
11:01 am comes in and tries to read the same property, will it wait
until the static constructor is finished? I'm hoping it does... If it
doesn't then the 2nd request will read dirty data from un-initialized
properties.

In my scenerio, the static constructor runs much quicker, but it's a
high traffic mission critical financial application that must read the
info each time correctly.

Thanks!
Dave
Oct 16 '08 #1
5 4442
What is it calculating? Can it be done offline (pre-aggregated outside of
the web app) and then run from the work done real time?

I see no reason to run long running processes in page code.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
"Dave" <ch********@yahoo.comwrote in message
news:10**********************************@w24g2000 prd.googlegroups.com...
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 makes sense. If a second (different) request at
11:01 am comes in and tries to read the same property, will it wait
until the static constructor is finished? I'm hoping it does... If it
doesn't then the 2nd request will read dirty data from un-initialized
properties.

In my scenerio, the static constructor runs much quicker, but it's a
high traffic mission critical financial application that must read the
info each time correctly.

Thanks!
Dave
Oct 16 '08 #2
Good question. I was asking that one myself.

According to documentation it will wait.
"A CLR type, such as a class or struct, can have a static constructor, which
can be used to initialize static data members. A static constructor will be
called at most once, and will be called before the first time a static
member of the type is accessed."

But i would check before trust it :)
George.
"Dave" <ch********@yahoo.comwrote in message
news:10**********************************@w24g2000 prd.googlegroups.com...
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 makes sense. If a second (different) request at
11:01 am comes in and tries to read the same property, will it wait
until the static constructor is finished? I'm hoping it does... If it
doesn't then the 2nd request will read dirty data from un-initialized
properties.

In my scenerio, the static constructor runs much quicker, but it's a
high traffic mission critical financial application that must read the
info each time correctly.

Thanks!
Dave
Oct 16 '08 #3
yes. the runtime takes a lock, so no code can access the class statics until
the constructor has completed. of course if it took an hour, asp.net would
try to kill the thread, which would not die until the constructor completed,
so a recycle would probably happen.
-- bruce (sqlwork.com)
"Dave" wrote:
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 makes sense. If a second (different) request at
11:01 am comes in and tries to read the same property, will it wait
until the static constructor is finished? I'm hoping it does... If it
doesn't then the 2nd request will read dirty data from un-initialized
properties.

In my scenerio, the static constructor runs much quicker, but it's a
high traffic mission critical financial application that must read the
info each time correctly.

Thanks!
Dave
Oct 16 '08 #4
Thanks Bruce,

That's what I figured. My static constructor doesn't really last an
hour, it's just a hypothetical situation (probably a bad choice of an
example). My static constructor really only should take about a second
or so. I'm simply trying to find out if all requests to the same
static property will wait for the static constructor to finish. This
happens to be very important for my app.

Cowboy, sorry I chose an hour; bad example.
George, I think you missed my point. I know the constructor will
finish before reading the static property. What I'm trying to find out
is if a 2nd (different) request for the same static property comes in
before the static constructor is finished executing from the first
request - what happens then? I would guess that it autamically
"locks" like bruce suggested, but if it didn't I'd have some issues.

Or to rephrase - I don't need to put any locking into my constructor
correct?

Big picture, I've always been a big fan of static methods and static
properties in an ASP.NET. Used correctly, it can really improve speed/
effeciency on high traffic sites. However, using static properties
you have to be carefull for multi-threaded locking issues. These
issues only arrise when you try and read and write to the same
memory. Therefore, I've avoided this by just having static properties
that are "get" only and methods with only local variables. The one
exception is in the static constructor where I "write" to these static
properties. This is what led me to this post.

Thanks!
Dave

On Oct 16, 12:48*pm, bruce barker
<brucebar...@discussions.microsoft.comwrote:
yes. the runtime takes a lock, so no code can access the class statics until
the constructor has completed. of course if it took an hour, asp.net would
try to kill the thread, which would not die until the constructor completed,
so a recycle would probably happen.

-- bruce (sqlwork.com)

"Dave" wrote:
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 makes sense. *If a second (different) request at
11:01 am comes in and tries to read the same property, will it wait
until the static constructor is finished? *I'm hoping it does... If it
doesn't then the 2nd request will read dirty data from un-initialized
properties.
In my scenerio, the static constructor runs much quicker, but it's a
high traffic mission critical financial application that must read the
info each time correctly.
Thanks!
Dave- Hide quoted text -

- Show quoted text -
Oct 16 '08 #5
you could also implement your own locking (inside the property), and load the
value on first reference. if you have static member that take time to load,
but not refenced a lot this may help.

also .net will call the static contructor on the first reference to the
static object (not at startup).

-- bruce (sqlwork.com)
"Dave" wrote:
Thanks Bruce,

That's what I figured. My static constructor doesn't really last an
hour, it's just a hypothetical situation (probably a bad choice of an
example). My static constructor really only should take about a second
or so. I'm simply trying to find out if all requests to the same
static property will wait for the static constructor to finish. This
happens to be very important for my app.

Cowboy, sorry I chose an hour; bad example.
George, I think you missed my point. I know the constructor will
finish before reading the static property. What I'm trying to find out
is if a 2nd (different) request for the same static property comes in
before the static constructor is finished executing from the first
request - what happens then? I would guess that it autamically
"locks" like bruce suggested, but if it didn't I'd have some issues.

Or to rephrase - I don't need to put any locking into my constructor
correct?

Big picture, I've always been a big fan of static methods and static
properties in an ASP.NET. Used correctly, it can really improve speed/
effeciency on high traffic sites. However, using static properties
you have to be carefull for multi-threaded locking issues. These
issues only arrise when you try and read and write to the same
memory. Therefore, I've avoided this by just having static properties
that are "get" only and methods with only local variables. The one
exception is in the static constructor where I "write" to these static
properties. This is what led me to this post.

Thanks!
Dave

On Oct 16, 12:48 pm, bruce barker
<brucebar...@discussions.microsoft.comwrote:
yes. the runtime takes a lock, so no code can access the class statics until
the constructor has completed. of course if it took an hour, asp.net would
try to kill the thread, which would not die until the constructor completed,
so a recycle would probably happen.

-- bruce (sqlwork.com)

"Dave" wrote:
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 makes sense. If a second (different) request at
11:01 am comes in and tries to read the same property, will it wait
until the static constructor is finished? I'm hoping it does... If it
doesn't then the 2nd request will read dirty data from un-initialized
properties.
In my scenerio, the static constructor runs much quicker, but it's a
high traffic mission critical financial application that must read the
info each time correctly.
Thanks!
Dave- Hide quoted text -
- Show quoted text -

Oct 17 '08 #6

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

Similar topics

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...
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...
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...
12
by: chandu | last post by:
hello, i want to know usage of static methods in a class. is it advantageous or disadvantage to use more static methods in a class. thank u
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...
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?
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
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
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
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...

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.