Is the code under App_code reentrant???

L

lander

That is, if multiple users are requesting or posting to the same page,
is the the code under app_code reentrant??? Why?

Thanks~!
 
A

Anthony Jones

lander said:
That is, if multiple users are requesting or posting to the same page,
is the the code under app_code reentrant??? Why?

I'm not sure you are using the term re-entrant appropriately I suspect you
mean is it threadsafe. The answer is: its up to you.

Normally classes that you may place in app_code that are then instanced by
ASPX pages will be threadsafe in that they are only being used by one thread
for the duration of the current request processing. However if you then
place instances such classes somewhere that can be accessed by more than one
thread (cache, static member etc) the responsibility for creating thread
safety is yours.
 
L

lander

I'm not sure you are using the term re-entrant appropriately I suspect you
mean is it threadsafe. The answer is: its up to you.

Normally classes that you may place in app_code that are then instanced by
ASPX pages will be threadsafe in that they are only being used by one thread
for the duration of the current request processing. However if you then
place instances such classes somewhere that can be accessed by more than one
thread (cache, static member etc) the responsibility for creating thread
safety is yours.

Ok, i create one variable inside the page class, which is static and
its value is set in page_load, is it safe to do so?
 
A

Anthony Jones

lander said:
Ok, i create one variable inside the page class, which is static and
its value is set in page_load, is it safe to do so?

No.

Static variables will be available across the whole app domain. It is
conceivable that two clients hitting the same page at the same time would
have two threads trying to modify that variable at the same time.

You will need to add some synchronising code such as:-

static SomeType ourValue = null;
static readonly object ourLock = new object();

public static void ValueMutator()
{
lock(ourLock);
{
// code that modified ourValue;
}
}

Of course the exact nature of the locking needed (if any) will depend on the
page logic.
 
L

lander

No.

Static variables will be available across the whole app domain. It is
conceivable that two clients hitting the same page at the same time would
have two threads trying to modify that variable at the same time.

You will need to add some synchronising code such as:-

static SomeType ourValue = null;
static readonly object ourLock = new object();

public static void ValueMutator()
{
lock(ourLock);
{
// code that modified ourValue;
}

}

Of course the exact nature of the locking needed (if any) will depend on the
page logic.

Thanks very much!
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top