Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ASP.NET, threading and static member.

16 views
Skip to first unread message

Francois

unread,
Dec 19, 2004, 11:12:31 PM12/19/04
to
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


Jon Skeet [C# MVP]

unread,
Dec 20, 2004, 4:22:08 AM12/20/04
to
Francois <fran...@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

Francois

unread,
Dec 20, 2004, 6:29:25 AM12/20/04
to
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:MPG.1c30aa174...@msnews.microsoft.com...

Daniel Jin

unread,
Dec 20, 2004, 9:05:02 AM12/20/04
to
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.

Scott Allen

unread,
Dec 20, 2004, 10:27:43 AM12/20/04
to
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/

Jon Skeet [C# MVP]

unread,
Dec 20, 2004, 12:05:08 PM12/20/04
to
Daniel Jin <Dani...@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.

Daniel Jin

unread,
Dec 20, 2004, 12:25:05 PM12/20/04
to
Learning something new every day, thanks Jon!

bruce barker

unread,
Dec 20, 2004, 12:48:27 PM12/20/04
to
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" <fran...@bettinghouses.com_NOSPAM> wrote in message
news:uQ3AXok5...@tk2msftngp13.phx.gbl...

Francois

unread,
Dec 20, 2004, 9:55:01 PM12/20/04
to
Thanks to all for all your information. :)

Best regards,

Francois.

"bruce barker" <nospam...@safeco.com> wrote in message
news:%2387adwr...@TK2MSFTNGP09.phx.gbl...

0 new messages