Application variable question (conversion from asp to dotnet)

G

Goober

I converted a website that was done in ASP to dotnet, and brought over a
number of items, some of which don't appear to work in the same way on
dotnet as they did on ASP.

One of them is an application variable.

I have this code in my global.asax in the Sub Session_Start:

Application.Lock() 'Add 1 to active visitor count when new visitor
Application("counter99") = Application("counter99") + 1
Application.UnLock()

It's a counter that I increment each time a user logged in to my old ASP
site.

I have this in the Sub Session_OnEnd to decrement the same counter:

Sub Session_OnEnd(ByVal sender As Object, ByVal e As EventArgs)
Application.Lock() 'Sub 1 active visitor count when new visitor
Application("counter99") = Application("counter99") - 1
Application.UnLock()
End Sub

I know that in ASP, you couldn't really rely on the session end to get a
"good" accurate count on users, but in ASP, it generally worked, and
gave you a reasonable estimate of how many users were hitting the site
at a time.

In dotnet, this counter registers a number exponentially higher than
what it did in ASP.

Is there a way I can implement a counter for current users of the
website that would be a tad more accurate than this?

Any help appreciated.

Thanks

BC
 
J

John Timney \(ASP.NET MVP\)

The code below should help you..

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

---somepage.aspx -----

<html>

<script language="VB" runat="server">
Sub Page_Load(Src as object, E as EventArgs)
MyGlobals.UserCount +=1
Response.Write(MyGlobals.UserCount.ToString())
End Sub

</script>
<body style="font: 10pt verdana">
Timneys Test
<form runat="server">
</form>


</body>
</html>


---global.asax----------

<%@ Application Classname="MyGlobals" %>
<script language="VB" runat="server">

public shared UserCount as integer = 1

Sub Application_OnStart()
' Application startup code goes here.
End Sub

Sub Session_OnStart()
' Session startup code goes here.

Application.Lock()
Application("SomeGlobalCounter") =
CType(Application("SomeGlobalCounter"),Integer) + 1
Application.UnLock()

End Sub

Sub Session_OnEnd()
' Session cleanup code goes here.
End Sub

Sub Application_OnEnd()
' Application cleanup code goes here.
End Sub

</script>
 

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

Similar Threads

Application Active users and global.asax 5
Sessions never end? IIS 5.1. 2
Sessions never end (again) - Please help 12
Session Question 6
Newb ASP question 1
Counting Sessions 4
Asp Session 0
ASP to ASP.NET conversion 2

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top