Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6549581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:06:36+00:00 2026-05-25T12:06:36+00:00

Consider the following extensions: public static class ReaderWriteExt { public static void ExecWriteAction(this ReaderWriterLockSlim

  • 0

Consider the following extensions:

public static class ReaderWriteExt
{
    public static void ExecWriteAction(this ReaderWriterLockSlim rwlock, Action action)
    {
        rwlock.EnterWriteLock();
        try
        {
            action();
        }
        finally
        {
            rwlock.ExitWriteLock();
        }
    }
    public static void ExecUpgradeableReadAction(this ReaderWriterLockSlim rwlock, Action action)
    {
        rwlock.EnterUpgradeableReadLock();
        try
        {
            action();
        }
        finally
        {
            rwlock.ExitUpgradeableReadLock();
        }
    }
}

Also consider the following sample usage (stripped of some supporting code):

private static ReaderWriterLockSlim _rwlock = new ReaderWriterLockSlim();
private static ... _cacheEntries = ....;

public static void RemoveEntry(string name)
{
    WeakReference outValue = null;
    _rwlock.ExecUpgradeableReadAction(() =>
        {                    
            if (_cacheEntries.TryGetValue(name, out outValue))
            {
                if (!outValue.IsAlive)
                {
                    _rwlock.ExecWriteAction(() => _cacheEntries.Remove(name));
                }
            }
        });
}

I’m new to C# coding and I was unable to find enough information about these topics that could guide me. To my question: I am considering using this concept in our production code, is it a bad idea? What can go wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:06:37+00:00Added an answer on May 25, 2026 at 12:06 pm

    That seems fine to me except that the code looks very cumbersome

    I would probably implement IDisposable as:

    public class WriteLock : IDisposable
    {
       ReaderWriterLockSlim _rwlock;
       public WriteLock(ReaderWriterLockSlim rwlock ) 
       { 
          _rwlock = rwlock;
          _rwlock.EnterWriteLock(); 
       }
       public void Dispose()
       {
          _rwlock.ExitWriteLock(); 
       }
    }
    

    Usage:

     private ReaderWriterLockSlim _rwlock = new ReaderWriterLockSlim();
    
     //...
    
     using (new WriteLock(_rwlock)) //<-- here the constructor calls EnterWriteLock
     {
          _cacheEntries.Remove(name);
    
     } //<---here Dispose method gets called automatically which calls ExitWriteLock
    

    Similarly, you can implement UpgradeableReadLock class implementing IDisposable interface.

    The idea is that you can create an instance of disposable class in using construct which ensures that in the constructor you enter into write lock by calling EnterWriteLock() method, and when it goes out of scope, Dispose() method is called automatically (by CLR) which calls ExitWriteLock() method.

    Note that it will not dispose ReaderWriterLockSlim object; it will dispose WriteLock object which is just a wrapper. ReaderWriterLockSlim will be as such in the user-class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider following program: static void Main (string[] args) { int i; uint ui; i
Consider the following: Public Module Extensions <Extension()> _ Public Sub Initialize(ByRef Target as SomeClass,
Consider the following extension method in c#, Traverse: IEnumerable<T> Traverse<T>( this IEnumerable<T> source, Func<T,
Consider the following directory structure: ./source/com/mypackage/../A.java ./extensions/extension1/source/com/mypackage/../T.java ./extensions/extension2/source/com/mypackage/../U.java ... ./extensions/extensionN/source/com/mypackage/../Z.java I want to produce
Consider the following HTML snippet: <p>Space&nbsp;Test</p> When this HTML is used in a web
Consider the following code: $(.lcontainer a).click(function () { var current = $(this); var name
Consider following scenario: I have RESTful URL /articles that returns list of articles user
Consider following SWT code example: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet151.java?view=co How can I separate the inline defined class?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.