Article View: borland.public.csharpbuilder.language.csharp
Article #577Object locking using ReaderWriterLock
From: Sunil.P.Thomas
Date: Thu, 12 Aug 2004 04:57
Date: Thu, 12 Aug 2004 04:57
102 lines
3178 bytes
3178 bytes
Hi all ,
I have a class like this , for controlling user access in our ASP.NET
application.
////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////
using System.Collections;
using System.Threading;
public class LoggedInUserManager
{
private static Hashtable _loggedUsers = new Hashtable();
private static ReaderWriterLock _readWriteLock = new ReaderWriterLock()
private const int WAIT_TIME = 10000 ;
public static void AddUser(int usn, string sessId )
{
_readWriteLock.AcquireWriterLock(WAIT_TIME);
try
{
if ( _loggedUsers.Contains(usn))
{
_loggedUsers.Remove(usn);
}
_loggedUsers.Add(usn, sessId);
}
catch{}
finally
{
_readWriteLock.ReleaseWriterLock();
}
}
public static void RemoveUser(int usn )
{
_readWriteLock.AcquireWriterLock(WAIT_TIME);
try
{
_loggedUsers.Remove(usn);
}
catch{}
finally
{
_readWriteLock.ReleaseWriterLock();
}
}
public static bool CanContinue(int usn , string sessionId )
{
bool result ;
_readWriteLock.AcquireReaderLock(WAIT_TIME);
try
{
result = _loggedUsers.Contains(usn) &&
_loggedUsers(usn).Equals(sessionId);
}
catch{}
finally
{
_readWriteLock.ReleaseReaderLock();
}
return result;
}
}
////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////
This class is used to restrict a user logging in to the system
simultaneously.
If he tries so , his first session will be reset.
There can be maximum 150 users in our site at a time. They usually loggin at
morning and logoff at evening.
So there will be a minimum 300 (AddUser() & RemoveUser()) updations for the
_loggedUsers collection
and indefinite number of reading of the _loggedUsers collection. (On each
page load CanContinue() is called ).
I want to know , is there any pitfalls in the above class ?
How we can avoid them ?
Any alternate solution ? (Initially we were using Application.Lock() , but
Marc told me that it will lock the entire application object , we are
looking for alternatives.)
Pls give your valuable suggestions.
Thanks in advance.
Regards
Sunil
Technical Details
Message-ID:
<411af77e$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.frii.net!newsfeed.frii.net!news.compaq.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 1efe35,c5e969dc4a2fb0d,start X-Google-Attributes: gid1efe35,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Sunil.P.Thomas" <sunil@netlinkindia.com> Newsgroups: borland.public.csharpbuilder.language.csharp Subject: Object locking using ReaderWriterLock Date: Thu, 12 Aug 2004 10:27:59 +0530 Lines: 108 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 NNTP-Posting-Host: 202.144.28.122 Message-ID: <411af77e$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1092286334 202.144.28.122 (11 Aug 2004 21:52:14 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.frii.net!newsfeed.frii.net!news.compaq.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail