I used the following class and .aspx code below to understand how static
works on variables and methods taken from
"http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfstaticpg.asp".
I opened up two instances of my browser to simulate two users accessing the
page at the same time by pointing to Employee.aspx below. Refreshing several
times, the counter value is increased and shared between both browser
instances.
If shared variables are shared between two instances of the app running in
two browsers, My question is what happens when a static method, that accepts
parameters, is called at the same time by two users. Are the parameter
values going to be isolated from each other? What about variable declared and
used within the static method itself? I'm trying to determine what are valid
rules for making a method static or things I need to be aware of when doing
so.
My code:
using System;
namespace StaticExample
{
/// <summary>
/// Summary description for Employee.
/// </summary>
public class Employee
{
public string id;
public string name;
public Employee ()
{
}
public Employee (string name, string id)
{
this.name = name;
this.id = id;
AddEmployee();
}
public static int employeeCounter;
public static int AddEmployee()
{
return ++employeeCounter;
}
}
}
The Employee.aspx page calls the above...
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
Employee employee;
employee = new Employee("Test User", "1");
Response.Write(Employee.employeeCounter);
} 4 1848
Dave,
In this case, you will want to place a lock statement around the
statement:
++employeeCounter;
The reason for this is that pages in ASP.NET are processed on different
threads. Because of this, you will need to protect the variable.
Static values are stored across the application domain. In ASP.NET, you
have one application domain running per web application, not per user
accessing the application. For this reason, you need to protect these
variables.
As for when to use static variables, in ASP.NET, I would use them when
you need to store application-wide information. It is even more important
in ASP.NET to make sure that the variable is protected for multithreaded
access (since pages are all going to be processed on different threads).
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Dave" <Da**@discussions.microsoft.com> wrote in message
news:3A**********************************@microsof t.com... I used the following class and .aspx code below to understand how static works on variables and methods taken from "http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfstaticpg.asp".
I opened up two instances of my browser to simulate two users accessing the page at the same time by pointing to Employee.aspx below. Refreshing several times, the counter value is increased and shared between both browser instances.
If shared variables are shared between two instances of the app running in two browsers, My question is what happens when a static method, that accepts parameters, is called at the same time by two users. Are the parameter values going to be isolated from each other? What about variable declared and used within the static method itself? I'm trying to determine what are valid rules for making a method static or things I need to be aware of when doing so.
My code:
using System;
namespace StaticExample { /// <summary> /// Summary description for Employee. /// </summary>
public class Employee { public string id; public string name;
public Employee () { }
public Employee (string name, string id) { this.name = name; this.id = id;
AddEmployee(); }
public static int employeeCounter;
public static int AddEmployee() { return ++employeeCounter; } } }
The Employee.aspx page calls the above...
private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here Employee employee; employee = new Employee("Test User", "1"); Response.Write(Employee.employeeCounter); }
Nicholas,
Thanks for the reply. To futher clarify an example of the method is:
A static method that accepts a ticker symbol as a string and makes and
HttpWebRequest to a url that returns a company description and stock prices
as Xml. Within this method, I then parse the XML and return only the filings
from the the method.
Just to clarify, what if two users call this at the same time? Since the
method is static, is any data within this method (parameters passed in and
local variables declared within the method) also considered static and shared
for all users of the class?
From what you are saying, I'm not sure when static would be a benefit to use
in ASP.NET.
"Nicholas Paldino [.NET/C# MVP]" wrote: Dave,
In this case, you will want to place a lock statement around the statement:
++employeeCounter;
The reason for this is that pages in ASP.NET are processed on different threads. Because of this, you will need to protect the variable.
Static values are stored across the application domain. In ASP.NET, you have one application domain running per web application, not per user accessing the application. For this reason, you need to protect these variables.
As for when to use static variables, in ASP.NET, I would use them when you need to store application-wide information. It is even more important in ASP.NET to make sure that the variable is protected for multithreaded access (since pages are all going to be processed on different threads).
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"Dave" <Da**@discussions.microsoft.com> wrote in message news:3A**********************************@microsof t.com...I used the following class and .aspx code below to understand how static works on variables and methods taken from "http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfstaticpg.asp".
I opened up two instances of my browser to simulate two users accessing the page at the same time by pointing to Employee.aspx below. Refreshing several times, the counter value is increased and shared between both browser instances.
If shared variables are shared between two instances of the app running in two browsers, My question is what happens when a static method, that accepts parameters, is called at the same time by two users. Are the parameter values going to be isolated from each other? What about variable declared and used within the static method itself? I'm trying to determine what are valid rules for making a method static or things I need to be aware of when doing so.
My code:
using System;
namespace StaticExample { /// <summary> /// Summary description for Employee. /// </summary>
public class Employee { public string id; public string name;
public Employee () { }
public Employee (string name, string id) { this.name = name; this.id = id;
AddEmployee(); }
public static int employeeCounter;
public static int AddEmployee() { return ++employeeCounter; } } }
The Employee.aspx page calls the above...
private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here Employee employee; employee = new Employee("Test User", "1"); Response.Write(Employee.employeeCounter); }
Dave,
In this case, if your method is not accessing other static variables,
and is only working with references/values that are stored on the stack (in
other words, declared in the function), then you should have nothng to worry
about. The only thing you have to worry about at this point is other
resources you might be accessing (for example, the same file, if you are
working with files).
As for the use of static, this sounds like a good one. It doesn't sound
like it needs any kind of specialization which would require individual
instances.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Dave" <Da**@discussions.microsoft.com> wrote in message
news:5E**********************************@microsof t.com... Nicholas,
Thanks for the reply. To futher clarify an example of the method is:
A static method that accepts a ticker symbol as a string and makes and HttpWebRequest to a url that returns a company description and stock prices as Xml. Within this method, I then parse the XML and return only the filings from the the method.
Just to clarify, what if two users call this at the same time? Since the method is static, is any data within this method (parameters passed in and local variables declared within the method) also considered static and shared for all users of the class?
From what you are saying, I'm not sure when static would be a benefit to use in ASP.NET.
"Nicholas Paldino [.NET/C# MVP]" wrote:
Dave,
In this case, you will want to place a lock statement around the statement:
++employeeCounter;
The reason for this is that pages in ASP.NET are processed on different threads. Because of this, you will need to protect the variable.
Static values are stored across the application domain. In ASP.NET, you have one application domain running per web application, not per user accessing the application. For this reason, you need to protect these variables.
As for when to use static variables, in ASP.NET, I would use them when you need to store application-wide information. It is even more important in ASP.NET to make sure that the variable is protected for multithreaded access (since pages are all going to be processed on different threads).
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"Dave" <Da**@discussions.microsoft.com> wrote in message news:3A**********************************@microsof t.com... >I used the following class and .aspx code below to understand how static > works on variables and methods taken from > "http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfstaticpg.asp". > > I opened up two instances of my browser to simulate two users accessing > the > page at the same time by pointing to Employee.aspx below. Refreshing > several > times, the counter value is increased and shared between both browser > instances. > > If shared variables are shared between two instances of the app running > in > two browsers, My question is what happens when a static method, that > accepts > parameters, is called at the same time by two users. Are the parameter > values going to be isolated from each other? What about variable > declared > and > used within the static method itself? I'm trying to determine what are > valid > rules for making a method static or things I need to be aware of when > doing > so. > > My code: > > using System; > > namespace StaticExample > { > /// <summary> > /// Summary description for Employee. > /// </summary> > > public class Employee > { > public string id; > public string name; > > public Employee () > { > } > > public Employee (string name, string id) > { > this.name = name; > this.id = id; > > AddEmployee(); > } > > public static int employeeCounter; > > public static int AddEmployee() > { > return ++employeeCounter; > } > } > } > > The Employee.aspx page calls the above... > > private void Page_Load(object sender, System.EventArgs e) > { > // Put user code to initialize the page here > Employee employee; > employee = new Employee("Test User", "1"); > Response.Write(Employee.employeeCounter); > }
Nicholas,
Thanks! One last thing. By converting other methods similar to this over to
static, what does one really gain besides less code in that I don't have to
create instances of each class? I'm guessing static uses less resources and
helps performance in the long run.
Dave.
"Nicholas Paldino [.NET/C# MVP]" wrote: Dave,
In this case, if your method is not accessing other static variables, and is only working with references/values that are stored on the stack (in other words, declared in the function), then you should have nothng to worry about. The only thing you have to worry about at this point is other resources you might be accessing (for example, the same file, if you are working with files).
As for the use of static, this sounds like a good one. It doesn't sound like it needs any kind of specialization which would require individual instances.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"Dave" <Da**@discussions.microsoft.com> wrote in message news:5E**********************************@microsof t.com... Nicholas,
Thanks for the reply. To futher clarify an example of the method is:
A static method that accepts a ticker symbol as a string and makes and HttpWebRequest to a url that returns a company description and stock prices as Xml. Within this method, I then parse the XML and return only the filings from the the method.
Just to clarify, what if two users call this at the same time? Since the method is static, is any data within this method (parameters passed in and local variables declared within the method) also considered static and shared for all users of the class?
From what you are saying, I'm not sure when static would be a benefit to use in ASP.NET.
"Nicholas Paldino [.NET/C# MVP]" wrote:
Dave,
In this case, you will want to place a lock statement around the statement:
++employeeCounter;
The reason for this is that pages in ASP.NET are processed on different threads. Because of this, you will need to protect the variable.
Static values are stored across the application domain. In ASP.NET, you have one application domain running per web application, not per user accessing the application. For this reason, you need to protect these variables.
As for when to use static variables, in ASP.NET, I would use them when you need to store application-wide information. It is even more important in ASP.NET to make sure that the variable is protected for multithreaded access (since pages are all going to be processed on different threads).
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"Dave" <Da**@discussions.microsoft.com> wrote in message news:3A**********************************@microsof t.com... >I used the following class and .aspx code below to understand how static > works on variables and methods taken from > "http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfstaticpg.asp". > > I opened up two instances of my browser to simulate two users accessing > the > page at the same time by pointing to Employee.aspx below. Refreshing > several > times, the counter value is increased and shared between both browser > instances. > > If shared variables are shared between two instances of the app running > in > two browsers, My question is what happens when a static method, that > accepts > parameters, is called at the same time by two users. Are the parameter > values going to be isolated from each other? What about variable > declared > and > used within the static method itself? I'm trying to determine what are > valid > rules for making a method static or things I need to be aware of when > doing > so. > > My code: > > using System; > > namespace StaticExample > { > /// <summary> > /// Summary description for Employee. > /// </summary> > > public class Employee > { > public string id; > public string name; > > public Employee () > { > } > > public Employee (string name, string id) > { > this.name = name; > this.id = id; > > AddEmployee(); > } > > public static int employeeCounter; > > public static int AddEmployee() > { > return ++employeeCounter; > } > } > } > > The Employee.aspx page calls the above... > > private void Page_Load(object sender, System.EventArgs e) > { > // Put user code to initialize the page here > Employee employee; > employee = new Employee("Test User", "1"); > Response.Write(Employee.employeeCounter); > } This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Steven D'Aprano |
last post by:
I've been doing a lot of reading about static methods in Python, and I'm
not exactly sure what they are useful for or why they were introduced.
Here is a typical description of them, this one...
|
by: heruti |
last post by:
Hi all...
I've been stumped by this for days. Bit of ASP code: (IIS)
Set LocalConn = CreateObject("ADODB.Connection")
LocalConn.CursorLocation = adUseClient
LocalConn.CommandTimeout = 0...
|
by: Richard Hengeveld |
last post by:
Hi all,
I'm trying to understand how pointers for function parameters work. As I
understand it, if you got a function like:
void f(int *i)
{
*i = 0;
}
|
by: Deniz Bahar |
last post by:
Hello,
A couple days ago my friend (OOP guy) shows me what OOP was all about
in C++. This morning I figured I can do pretty much the same thing
with C (by putting function pointers in...
|
by: Ron Weldy |
last post by:
I have a test server runinng 2003/IIS 6 with a mixture of asp and asp.net
files. On my workstation I have a share set up to the folder where the web
files reside. I am just doing quick and dirty...
|
by: Michael B. Trausch |
last post by:
Hello everyone,
I am having a problem with static class properties that I suspect is somewhat
trivial, but I don't understand what is wrong. I am using GNU C++ on
Windows, though I am (hoping)...
|
by: BLUE |
last post by:
FirstClass members:
- static int counter;
- SingletonClass sc = SingletonClass.Instance;
Moreovere FirstClass uses a static class named SecondClass with a static
property...
|
by: rbukkara |
last post by:
Hi,
I have got the following error while trying to add a user in the LDAP Directory.
javax.naming.NameNotFoundException: ; remaining name 'uid=vassila,ou=People,dc=cs,dc=uno,dc=edu'
I have...
|
by: spectrumdt |
last post by:
Hello.
I am trying to extend Python with some C code. I made a trivial
"Hello World" program in C that I am trying to wrap in "boilerplate"
for inclusion in a Python program. But I can't compile...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |