ASP.NET, threading and static member.

F

Francois

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
 
J

Jon Skeet [C# MVP]

Francois said:
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.
 
F

Francois

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
 
G

Guest

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 said:
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
 
S

Scott Allen

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/

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
 
J

Jon Skeet [C# MVP]

Daniel Jin said:
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.
 
G

Guest

Learning something new every day, thanks Jon!

Jon Skeet said:
Daniel Jin said:
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.
 
B

bruce barker

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)



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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top