Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / October 2005

Tip: Looking for answers? Try searching our database.

Please save me from shooting myself in the head!

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
William Sullivan - 18 Oct 2005 22:36 GMT
I'm dying here...  I have an object that I use to access objects in the
session state.  I may be reading and writing at the same time, so I am trying
to make the accessors thread safe using ReaderWriter locks.  Here's a
stripped down example of my class:

Class SessionLogic{
private static ReaderWriterLock myObjectLock = new ReaderWriterLock();

...

public MyObject MyObject{
 get
 {
     MyObject foo = null;
     myObjectLock.AcquireReaderLock(5000);
     try
     {
      foo = this.session["MyObject"] as MyObject;
      try
      {                       
         LockCookie monster = myObjectLock.UpgradeToWriterLock(5000);   
         try
         {
             foo = new MyObject();   
             this.session["MyObject"]  = foo;               
         }
         finally
         {
               myObjectLock.DowngradeFromWriterLock(ref monster);
         }
      }       
      catch (Exception ex)
      {
          string message = ex.Message;  // just so I can see
      }
    finally
    {
        myObjectLock.ReleaseReaderLock();
    }
    return foo;
  }
}
}

This structure is almost word for word out of MS' documentation on
ReaderWriterLock.  Yet whenever I run it, I get an error:  "Attempt to
release mutex not owned by caller."  What the...  Its the ONLY CALLER!  What
is happening here????
Bruce Barker - 18 Oct 2005 23:26 GMT
not sure why you are doing this. add/getting references from session are
thread safe. so there is no need for the locking code. also asp.net
serializes access to the session by only running one request at a time for
the same session.

now to the problem with your code, you use a global lock so any thread
accessing the session object (even though the session objects are different
for threads processing diffent sessions) must take a lock and release. the
lock is created under the creditials of the user of the first request, so
other users will not be able to access it unless you change the security of
the mutex, to allow read/write from anyone. turn off authenication to see
this.

-- bruce (sqlwork.com)

> I'm dying here...  I have an object that I use to access objects in the
> session state.  I may be reading and writing at the same time, so I am
[quoted text clipped - 46 lines]
> What
> is happening here????
William Sullivan - 19 Oct 2005 13:36 GMT
Thank you, my head now remains whole.  

> not sure why you are doing this. add/getting references from session are
> thread safe. so there is no need for the locking code. also asp.net
[quoted text clipped - 61 lines]
> > What
> > is happening here????

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.