One person at a time...

D

deostroll

Is there any way I can write a asp script such that if one user is
using it no other person can access that page until the processing is
complete...?
--deostroll
 
B

Bob Barrows [MVP]

deostroll said:
Is there any way I can write a asp script such that if one user is
using it no other person can access that page until the processing is
complete...?
--deostroll

Just to be clear, there is no such thing as an "asp script". ASP is a
"platform" that supports the execution of various scripting languages,
including vbscript and jscript. I will assume you are talking about
vbscript, although the answer is similar for jscript. There are several
options, but i think the simplest is to utilize an Application variable:

<%
application.lock
application("I_am_locked") = true
application.unlock

'do stuff

application.lock
application("I_am_locked") = false
application.unlock
%>

You can find the ASP Scripting documentation here:
http://msdn.microsoft.com/library/en-us/iissdk/html/2c40c3cf-90eb-41ca-ae2a-0ef33a651779.asp
 
B

Bob Barrows [MVP]

deostroll said:
Is there any way I can write a asp script such that if one user is
using it no other person can access that page until the processing is
complete...?
--deostroll
Oh wait, it's a little more complex than that. I will reply later with a
better reply if nobody beats me to it.
 
D

deostroll

Oh wait, it's a little more complex than that. I will reply later with a
better reply if nobody beats me to it.
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Hey bob, what if I just had a text file (locked.txt) that would read
either a 'yes' or 'no'. No in my asp code I could do some ordinary
file handling to access that text file. I can read the content of the
text file. If it reads 'no' then I can

a) truncate the text file and write yes into it, and
b) go on with the processing

or else give a message - "sorry this page is locked!!!"?
 
B

Bob Barrows [MVP]

deostroll said:
Is there any way I can write a asp script such that if one user is
using it no other person can access that page until the processing is
complete...?
--deostroll

I think this will work better. I've incorporated a 15 second timeout - you
can adjust it to whatever fits your scalability needs:

<%
dim guid, TypeLib, start, t
'generate a unique id (GUID)
Set TypeLib = CreateObject("Scriptlet.TypeLib")
guid=Left(CStr(TypeLib.Guid), 38)

start=Now()
Do Until LockMe(guid) or t>15
t=DateDiff("s",start, Now())
Loop

'do stuff

UnLockMe

Function LockMe(pGuid)
if application("LockedBy") <> "" then
LockMe=false
else
application.lock
if application("LockedBy") <> "" then
LockMe=false
Application.Unlock
else
application("LockedBy") = pGuid
application.unlock
LockMe=true
end if
end if
End Function

Sub UnLockMe()
application.lock
application("LockedBy") = ""
application.unlock
End Sub
%>
 
B

Bob Barrows [MVP]

deostroll said:
Hey bob, what if I just had a text file (locked.txt) that would read
either a 'yes' or 'no'. No in my asp code I could do some ordinary
file handling to access that text file. I can read the content of the
text file. If it reads 'no' then I can

a) truncate the text file and write yes into it, and
b) go on with the processing

or else give a message - "sorry this page is locked!!!"?

It's more complicated than that. What if another thread accesses the text
file before you save your change to it? The other thread will still see that
it says "no", change it to "yes" and go on its merry way.

You could utilize a GUID the way i did in my second example. Of course, with
text files, you need to worry about file-system permissions. Plus
performance: disk i/o is slower than accessing application variables.
 
D

deostroll

It's more complicated than that. What if another thread accesses the text
file before you save your change to it? The other thread will still see that
it says "no", change it to "yes" and go on its merry way.

You could utilize a GUID the way i did in my second example. Of course, with
text files, you need to worry about file-system permissions. Plus
performance: disk i/o is slower than accessing application variables.
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Thanx bob, that was some real help.

-deostroll
 
A

Anthony Jones

Bob Barrows said:
I think this will work better. I've incorporated a 15 second timeout - you
can adjust it to whatever fits your scalability needs:

<%
dim guid, TypeLib, start, t
'generate a unique id (GUID)
Set TypeLib = CreateObject("Scriptlet.TypeLib")
guid=Left(CStr(TypeLib.Guid), 38)

start=Now()
Do Until LockMe(guid) or t>15
t=DateDiff("s",start, Now())
Loop

'do stuff

UnLockMe

Function LockMe(pGuid)
if application("LockedBy") <> "" then
LockMe=false
else
application.lock
if application("LockedBy") <> "" then
LockMe=false
Application.Unlock
else
application("LockedBy") = pGuid
application.unlock
LockMe=true
end if
end if
End Function

Sub UnLockMe()
application.lock
application("LockedBy") = ""
application.unlock
End Sub
%>

Bob,

I'm not sure I understand why you're using a GUID? Session.SessionID or
some other ID that would help identify the user, client or session currently
locking for diagnostic purposes would be better.

Also after your code times out failing to get a lock the code goes on to to
it's stuff anyway.
 
B

Bob Barrows [MVP]

Bob,

I'm not sure I understand why you're using a GUID? Session.SessionID
or some other ID that would help identify the user, client or session
currently locking for diagnostic purposes would be better.

Not everyone uses sessions. GUIDs can be used whether sessions are enabled
or not.

I was just trying to create a generic solution, trusting that the OP would
be able to adapt it to his situation. To amplify:

If you're using sessions, and have taken precautions against
session-hijacking, use SessionID instead of Guid
If you are on a LAN or WAN, and your site has anonymous disabled and
Integrated autentication enabled, use the user's login name instead of the
GUID
If you are using Basic Authentication and your user logs into a database
that returns a unique user id, use the id instead of the GUID

'do stuff
Also after your code times out failing to get a lock the code goes on
to to it's stuff anyway.

Oops ... er ... I left that as an exercise for the reader ... yeah, that's
the ticket!
Come on reader (not you, Anthony - I know you know the answer <grin>)! What
has to be changed in the above snippet to make it not go to the "do stuff"
part!
 

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

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top