Question about scope of static member variable

  • Thread starter Thread starter newjazzharmony
  • Start date Start date
N

newjazzharmony

All,

When I create a class with a static member variable and reference that
class in a console applicaton it appears as though each instance of the
application has its own separate copy of the member variable.

Therefore, it would appear that the scope of a static class member is
limited to a specific instance of an application that happens to be
running on the machine.

Is this correct?

Thanks,

--Jonathan
 
When I create a class with a static member variable and reference that
class in a console applicaton it appears as though each instance of the
application has its own separate copy of the member variable.

Therefore, it would appear that the scope of a static class member is
limited to a specific instance of an application that happens to be
running on the machine.

Is this correct?

Every application runs in its own address space with its own variables,
unless you work hard (via shared memory or some other technique) to make
it otherwise.

-- Barry
 
Hello, Barry!

BK> Every application runs in its own address space with its own variables,
BK> unless you work hard (via shared memory or some other technique) to
BK> make it otherwise.

Scope of static var can be limited to the scope of the thread via ThreadStaticAttribute.
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
Vadym Stetsyak said:
Hello, Barry!

BK> Every application runs in its own address space with its own variables,
BK> unless you work hard (via shared memory or some other technique) to
BK> make it otherwise.

Scope of static var can be limited to the scope of the thread via ThreadStaticAttribute.

The OPs question relates to multiple instances of a console application
not sharing the value of a static variable.

-- Barry
 
Hello, Barry!

BK> The OPs question relates to multiple instances of a console application
BK> not sharing the value of a static variable.

I've metioned that static variable can have not only application scope, but also thread scope. This notion corrects your post that it is not necessary to "work hard" to "make it otherwise". :8-)

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
Every application runs in its own address space with its own variables,
unless you work hard (via shared memory or some other technique) to make
it otherwise.

Barry,

Thanks for the response!

The MSDN documentation seems to suggest that the scope of a static
member extends to a "App Domain".
Does this mean that every instance of a console app running on a
machine lives in a different App Domain?

How does the scenario change if it's an ASP.Net application?
Does every user who accesses a web application get an individual copy
of a static variable as well (or do all users of the web app share the
same app domain)?

Thanks again,

-Jonathan
 
How does the scenario change if it's an ASP.Net application?
Does every user who accesses a web application get an individual copy
of a static variable as well (or do all users of the web app share the
same app domain)?

I just answered my question with another experiment.
It appears that all users of the web application are now accessing the
same copy of the static variable.
I guess this means all instances of a web application are in the same
AppDomain.

Thanks,

Jonathan
 
Vadym Stetsyak said:
Hello, Barry!

BK> The OPs question relates to multiple instances of a console application
BK> not sharing the value of a static variable.

I've metioned that static variable can have not only application scope, but
also thread scope. This notion corrects your post that it is not necessary to
"work hard" to "make it otherwise". :8-)

Well, you have to work hard to make the static variable share its
instance between applications. I'll be more clear next time.

-- Barry
 
I just answered my question with another experiment.
It appears that all users of the web application are now accessing the
same copy of the static variable.
I guess this means all instances of a web application are in the same
AppDomain.

That is a fairly configurable aspect of ASP.NET on Win2K3 - the domains
get recycled, etc.

-- Barry
 
That is a fairly configurable aspect of ASP.NET on Win2K3 - the domains
get recycled, etc.

-- Barry

You have to be careful if you rely upon a shared variable within asp.net
as the web server can still choose to load a new app domain if that is
easier for processing threads. It can also remove one to free memory if
it hasn't been used in a while.
 
You have to be careful if you rely upon a shared variable within asp.net
as the web server can still choose to load a new app domain if that is
easier for processing threads. It can also remove one to free memory if
it hasn't been used in a while.

Thanks for the reply!
I want to share one instance of an object across an app domain (in an
ASP dot net application), and I'm trying to weigh the pros and cons of
the following two approaches:

a) Storing the object in ApplicationState
b) Storing the object in a static member variable (utilizing a
singleton design pattern)

Any recommendations on which method would be more suitable?
Are there any other approaches that might be better?

The object in question is a dot net class wrapping a COM component.
The class creates an SSL session to a proprietary device that manages
encryption and decryption.

Thanks,
Jonathan
 
Back
Top