473,569 Members | 2,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Q about static members in asp.net (C#)

Hi im trying to prevent people from useing more than one browser to view my
page.

But first i need to know excatly how static members of a form class is
behaving.

When is the IntanceCounter (see ***) desctructet?(re set) When the IIS server
is restaret? Then Browser close? Im getting diffrent results everytime i try
this.

I have something like this:
----------------------------
The ASPX
----------------------------
<form id="Form1" method="post" runat="server">
<% Response.Write( WriteHtml()); %>
</form>

----------------------------
codebehind
----------------------------
static int Instancecounter =0; // *** Here is the instanceCounter

public string WriteHtml()
{
if(counter == 0)
{
Instancecounter ++;
return "The HTML to render";
}
else
{
return "Only one browser please";
}
}

Nov 18 '05 #1
10 1391
It is generally very different to determine the current active number
of user connected to your web server, unless your users will always
signoff from you website before they exit, and you know that's not
always the case.

When a user close their browser, it does not inform the web server.
You could use javascript to trigger a postback to the web server when
the page unloads, however, it will not always work. One example is
when the user terminate the "Internet Explorer" process in task
manager.

From you code sample, the "IntanceCounter " field is shared among all
objects of the class, so it will not get reset until the ASP.NET
worker process restarts.

There are indirect ways to limit only one browser accessing your
website per machine. However, it will be helpful if you could explain
why you would want to do that.

A web server is designed to serve large amount of requests
simultaneously, and it does this by release the connection from the
client as soon as the request is processed, and that is why it is very
difficult to perform the functionality you're looking for.

Tommy,

"Flare" <None> wrote in message news:<#3******* *******@TK2MSFT NGP12.phx.gbl>. ..
Hi im trying to prevent people from useing more than one browser to view my
page.

But first i need to know excatly how static members of a form class is
behaving.

When is the IntanceCounter (see ***) desctructet?(re set) When the IIS server
is restaret? Then Browser close? Im getting diffrent results everytime i try
this.

I have something like this:
----------------------------
The ASPX
----------------------------
<form id="Form1" method="post" runat="server">
<% Response.Write( WriteHtml()); %>
</form>

----------------------------
codebehind
----------------------------
static int Instancecounter =0; // *** Here is the instanceCounter

public string WriteHtml()
{
if(counter == 0)
{
Instancecounter ++;
return "The HTML to render";
}
else
{
return "Only one browser please";
}
}

Nov 18 '05 #2
> From you code sample, the "IntanceCounter " field is shared among all
objects of the class, so it will not get reset until the ASP.NET
worker process restarts.
Ok. I forget that idea.
There are indirect ways to limit only one browser accessing your
website per machine. However, it will be helpful if you could explain
why you would want to do that.


Because some guy in the company has used session keys to track servel
navigation infomation and other "intance" specfic detalis. So when the user
open another window he suddenly os manipulation the same sesison object from
2 places. This of course gives bizrar results. Thats why i want a way to
permit only one browser per. user / session.

There is no way of chaning excisting code other than minor changes,,,any
sugestions?

Anders
Nov 18 '05 #3
Hi Flare,

Thank you for posting to the MSDN newsgroups.

I am interested in this issue and researching on it now. I will update you
as soon as possible.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 18 '05 #4
> Thank you for posting to the MSDN newsgroups.

I am interested in this issue and researching on it now. I will update you
as soon as possible.


Thx Jacob.

Anders Jacobsen, Denmark
Nov 18 '05 #5
Hi Anders Jacobsen,

Firstly I want to confirm with you on the static members of a class. A
static member of a class belongs to the type itself rather than to a
specific object. That is, a static member shares among all the instances of
a class in the same process. In this case, the process denotes certain
aspnet_wp.exe, where your web application resides in. only when the
appropriate worker process was recycled will those static members be reset.

As for your concern, you can hook the window close event and then submit a
postback to the server when the window is to be closed. On server side, you
can expire the current session. As a consequence, next time a new session
should be created, instead of using the previous one.

I hope it helps.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 18 '05 #6
> Firstly I want to confirm with you on the static members of a class. A
static member of a class belongs to the type itself rather than to a
specific object. That is, a static member shares among all the instances of a class in the same process. In this case, the process denotes certain
aspnet_wp.exe, where your web application resides in. only when the
appropriate worker process was recycled will those static members be reset.

K. Nice to clear that out.
As for your concern, you can hook the window close event and then submit a
postback to the server when the window is to be closed. On server side, you can expire the current session. As a consequence, next time a new session
should be created, instead of using the previous one.


But wouldnt that destroy the current session? I mean I surfe the site, then
press CTRL-N to open a new window. Would that "Kill" the current session. I
want the ONLY the curent sesision, and that session should only be avaible
in the browser window that opened it. Did I misunderstand your suggestion?

Anders
Nov 18 '05 #7
Hi Anders,

I have done a lot of research on this issue. I agree with you that your
concern is correct. It seems that there is not an easy/direct way to do
what you want.

I have another idea for your reference:

1. Add an ActiveX control/COM compent to the web application or call a
local (client side) application in the web application.

2. On the client side, we can do some programming to make sure that an
ActiveX control/COM component/application only is started one instance.

I hope it helps.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 18 '05 #8
> I have done a lot of research on this issue. I agree with you that your
concern is correct. It seems that there is not an easy/direct way to do
what you want.


M2 :) And thx for your interrest in the issue. Your other solution dosent
seem very atractive to me, anyway not to our problem. I have started
thinking about if I could use the OnBrowserClose event to decrement a static
counter. But now im on deep water. Can I get in "contact" with the server
side Form objects static members.? And I would be the big trouble if the
OnBrowserClose event didnt fire. Eg. the browser craches. Then the
intanceCounter would stay at 1 and the next time i opened a "fresh" window i
would not be allowed to see my page because the instancer counter still was
one.

Hmmmmmm......

Anders, Denmark,
Elsam Engineering
Nov 18 '05 #9
Hi Anders,

Thank you for your update.

I have done more research regarding this issue and have another idea for
your reference.

We can try to do this with window.open("UR L","WindowName" ) and then use the
new window which has a name.

For example, suppose he has a logon page. That page would call
window.open("Ho mePage.aspx","M yApp").

Then, each page in the application would have a hidden field and some
client-side script that copies the window.name into the hidden field. The
hidden field gets posted to the server with each POST (but this doesn't
cover GET requests). On the server, verify that the hidden field has the
value "MyApp".

As for the original window, either set its location.href to a "Please close
me" page, or use document.write to print "please close me" onto the screen.

The code would be something like this:

window.open("ht tp://michmo1/ntest1/Continue.aspx", "MyApp")
window.location .href = "http://michmo1/ntest1/PleaseClose.asp x"
OR
window.open("ht tp://michmo1/ntest1/Continue.aspx", "MyApp")
document.write( "Please close this window")

I hope it helps.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3587
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming non-standard C++ or if the problem lies elsewhere. To summarize static const class members are not being accessed properly when accessed from a...
8
4569
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member: VarArray::funct were an extern, but it is declared in the same file (q.v.). What is the remedy for this? =================
15
6569
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and either has access only to static members of the class (ie. assuming no object of the class is in scope - neither by arguments recieved nor by local...
6
2463
by: lovecreatesbeauty | last post by:
Hello Experts, Why static data members can be declared as the type of class which it belongs to? Inside a class, non-static data members such as pointers and references can be declared as type of its own class. Non-static data members can not be declared as type of its own class (excluding pointers and references).
13
7692
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. They don't actually protect any encapsulation or anything, and for all the actual protection they offer, they might as well be public. For...
3
9733
by: Mauzi | last post by:
hi, this may sound odd and noob like, but what is the 'big' difference between static and non-static funcitons ? is there any performace differnce? what is the best way to use them ? thnx mauzi
6
1616
by: Matt | last post by:
All of a sudden all my C# apps require the keyword static on all global fields and methods that I create. Even in the simplest of console apps. Can someone tell me what I have inadvertenly set in the IDE or did something else happen to cause this? I get the following error with the sample code below: ...
11
2230
by: dee | last post by:
OleDbCommand class like many .NET classes has the following description in its help file: "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Any instance members are not guaranteed to be thread safe." I have 2 questions: 1. I thought dynamic variables are thread-safe...
11
3810
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of...
8
2879
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7970
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.