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

ASP.NET, threading and static member.

Hi all,

Each time an HTTP request is received by IIS, it forwards it to the ASP.NET
worker process. Then does the ASP.NET worker process in turn start a new
thread for each HTTP request it serves (from a pool of thread for example).
Or are all HTTP requests served by the same thread?

An other question is related to static members. A static member in a class
will have the same value across all its instance. But for example if 2
instances of the class are in 2 different threads, will the static member
still share the value or not? If I remember well, in Java, static member do
not share the same value between different threads and I would like to know
if it is the same thing in C# or not.

If anyone got a good link explaining one of both of the 2 topics above that
would be great too !

Thanks in advance,

Francois
Nov 16 '05 #1
8 1412
Francois <fr******@bettinghouses.com_NOSPAM> wrote:
Each time an HTTP request is received by IIS, it forwards it to the ASP.NET
worker process. Then does the ASP.NET worker process in turn start a new
thread for each HTTP request it serves (from a pool of thread for example).
Or are all HTTP requests served by the same thread?
I believe they're served by the thread-pool.
An other question is related to static members. A static member in a class
will have the same value across all its instance.
It's not that it's shared across all instances - it's that the member
belongs to the type rather than to any instance.
But for example if 2
instances of the class are in 2 different threads, will the static member
still share the value or not? If I remember well, in Java, static member do
not share the same value between different threads and I would like to know
if it is the same thing in C# or not.


It's the same in C# as in Java, but you remember Java incorrectly.
Static members are shared across threads.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Tx a lot Jon,

Do you know if there is there any way to set some sort of a type value that
will be limited to the scope of a thread only? or is that impossible to do?

Tx in advance and tx again for your answer.

Francois
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Francois <fr******@bettinghouses.com_NOSPAM> wrote:
Each time an HTTP request is received by IIS, it forwards it to the ASP.NET worker process. Then does the ASP.NET worker process in turn start a new
thread for each HTTP request it serves (from a pool of thread for example). Or are all HTTP requests served by the same thread?


I believe they're served by the thread-pool.
An other question is related to static members. A static member in a class will have the same value across all its instance.


It's not that it's shared across all instances - it's that the member
belongs to the type rather than to any instance.
But for example if 2
instances of the class are in 2 different threads, will the static member still share the value or not? If I remember well, in Java, static member do not share the same value between different threads and I would like to know if it is the same thing in C# or not.


It's the same in C# as in Java, but you remember Java incorrectly.
Static members are shared across threads.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
You can keep thread specific data by using SetData and GetData static
methods. to give the appearance that it's part of the type, you can use a
property to wrap that logic.

"Francois" wrote:
Tx a lot Jon,

Do you know if there is there any way to set some sort of a type value that
will be limited to the scope of a thread only? or is that impossible to do?

Tx in advance and tx again for your answer.

Francois
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Francois <fr******@bettinghouses.com_NOSPAM> wrote:
Each time an HTTP request is received by IIS, it forwards it to the ASP.NET worker process. Then does the ASP.NET worker process in turn start a new
thread for each HTTP request it serves (from a pool of thread for example). Or are all HTTP requests served by the same thread?


I believe they're served by the thread-pool.
An other question is related to static members. A static member in a class will have the same value across all its instance.


It's not that it's shared across all instances - it's that the member
belongs to the type rather than to any instance.
But for example if 2
instances of the class are in 2 different threads, will the static member still share the value or not? If I remember well, in Java, static member do not share the same value between different threads and I would like to know if it is the same thing in C# or not.


It's the same in C# as in Java, but you remember Java incorrectly.
Static members are shared across threads.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #4
Hi Francois:

I'm not sure what you ultimately want to accomplish, but in ASP.NET
you generally want a value around that is tied to a specific request,
not a specific thread. To do this you can use the Context.Items
collection.

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

On Mon, 20 Dec 2004 18:29:25 +0700, "Francois"
<fr******@bettinghouses.com_NOSPAM> wrote:
Tx a lot Jon,

Do you know if there is there any way to set some sort of a type value that
will be limited to the scope of a thread only? or is that impossible to do?

Tx in advance and tx again for your answer.

Francois
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft. com...
Francois <fr******@bettinghouses.com_NOSPAM> wrote:
> Each time an HTTP request is received by IIS, it forwards it to theASP.NET > worker process. Then does the ASP.NET worker process in turn start a new
> thread for each HTTP request it serves (from a pool of thread forexample). > Or are all HTTP requests served by the same thread?


I believe they're served by the thread-pool.
> An other question is related to static members. A static member in aclass > will have the same value across all its instance.


It's not that it's shared across all instances - it's that the member
belongs to the type rather than to any instance.
> But for example if 2
> instances of the class are in 2 different threads, will the staticmember > still share the value or not? If I remember well, in Java, static memberdo > not share the same value between different threads and I would like toknow > if it is the same thing in C# or not.


It's the same in C# as in Java, but you remember Java incorrectly.
Static members are shared across threads.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #5
Daniel Jin <Da*******@discussions.microsoft.com> wrote:
You can keep thread specific data by using SetData and GetData static
methods. to give the appearance that it's part of the type, you can use a
property to wrap that logic.


Alternatively, use the [ThreadStatic] attribute
(ThreadStaticAttribute). It's only available on static variables
(AFAICT), but it keeps one variable per thread.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Learning something new every day, thanks Jon!

"Jon Skeet [C# MVP]" wrote:
Daniel Jin <Da*******@discussions.microsoft.com> wrote:
You can keep thread specific data by using SetData and GetData static
methods. to give the appearance that it's part of the type, you can use a
property to wrap that logic.


Alternatively, use the [ThreadStatic] attribute
(ThreadStaticAttribute). It's only available on static variables
(AFAICT), but it keeps one variable per thread.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #7
asp.net not only uses a thread pool, but may switch threads during request
processing (thread agile). this makes thread local storage unreliable.

in .net, static data is shared across all thread (unless the ThreadStatic
attribute is used, which then uses TLS). due to aps.net thread agility
mentioned above, you can not use this method.

-- bruce (sqlwork.com)

"Francois" <fr******@bettinghouses.com_NOSPAM> wrote in message
news:uQ**************@tk2msftngp13.phx.gbl...
| Hi all,
|
| Each time an HTTP request is received by IIS, it forwards it to the
ASP.NET
| worker process. Then does the ASP.NET worker process in turn start a new
| thread for each HTTP request it serves (from a pool of thread for
example).
| Or are all HTTP requests served by the same thread?
|
| An other question is related to static members. A static member in a class
| will have the same value across all its instance. But for example if 2
| instances of the class are in 2 different threads, will the static member
| still share the value or not? If I remember well, in Java, static member
do
| not share the same value between different threads and I would like to
know
| if it is the same thing in C# or not.
|
| If anyone got a good link explaining one of both of the 2 topics above
that
| would be great too !
|
| Thanks in advance,
|
| Francois
|
|
Nov 16 '05 #8
Thanks to all for all your information. :)

Best regards,

Francois.

"bruce barker" <no***********@safeco.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
asp.net not only uses a thread pool, but may switch threads during request
processing (thread agile). this makes thread local storage unreliable.

in .net, static data is shared across all thread (unless the ThreadStatic
attribute is used, which then uses TLS). due to aps.net thread agility
mentioned above, you can not use this method.

-- bruce (sqlwork.com)

"Francois" <fr******@bettinghouses.com_NOSPAM> wrote in message
news:uQ**************@tk2msftngp13.phx.gbl...
| Hi all,
|
| Each time an HTTP request is received by IIS, it forwards it to the
ASP.NET
| worker process. Then does the ASP.NET worker process in turn start a new
| thread for each HTTP request it serves (from a pool of thread for
example).
| Or are all HTTP requests served by the same thread?
|
| An other question is related to static members. A static member in a class | will have the same value across all its instance. But for example if 2
| instances of the class are in 2 different threads, will the static member | still share the value or not? If I remember well, in Java, static member
do
| not share the same value between different threads and I would like to
know
| if it is the same thing in C# or not.
|
| If anyone got a good link explaining one of both of the 2 topics above
that
| would be great too !
|
| Thanks in advance,
|
| Francois
|
|

Nov 16 '05 #9

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

Similar topics

3
by: Savagesmc | last post by:
Problem Background: I am working on a thin wrapper for the pthreads interface on linux, and have encountered frustrating behavior. The idea is to have a "PosixThread" class that any class can...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
4
by: Bardo | last post by:
Hi, I have a situation where I am capturing both a WMI event utilising the "ManagementEventWatcher" in the "System.Management" namespace, and a corresponding event ("EntryWritten") raised from...
2
by: Howard Swope | last post by:
Could someone help explain thread safety issues in the System.Collections classes? The documentation states:...
6
by: MPH Computers | last post by:
Hi I am looking for some help on Threading and Critical Sections I have a main thread that controls an event the event handler creates a new thread for carrying out the work because the...
8
by: Francois | last post by:
Hi all, Each time an HTTP request is received by IIS, it forwards it to the ASP.NET worker process. Then does the ASP.NET worker process in turn start a new thread for each HTTP request it...
8
by: Lothar Behrens | last post by:
Hi, I am thinking about using classes to encapsulate threads for my application. My requirements are the following: The thread implementation sould not know what has to be implemented in...
9
by: cgwalters | last post by:
Hi, I've recently been working on an application which does quite a bit of searching through large data structures and string matching, and I was thinking that it would help to put some of this...
13
by: Alexander Gnauck | last post by:
Hello, while using async sockets I ran into a strange problem which occurs only on some machines. I wrote a small demo application which can be used to reproduce the problem. You can download it...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.