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

  • Home
  • SEARCH
  • 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 6887573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:54:05+00:00 2026-05-27T05:54:05+00:00

How can I replace this code: try { lock (lockForSomethingInUse) { somethingInUse = true;

  • 0

How can I replace this code:

try
{
    lock (lockForSomethingInUse)
    {
        somethingInUse = true;
        // use it
    }
}
finally
{
    somethingInUse = false;
}

With:

using (lockForSomething)
{
   // use it
}
  • 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-27T05:54:06+00:00Added an answer on May 27, 2026 at 5:54 am

    If you want to use lock and be able to know whether lock is actually acquired at the moment with using only one statement/variable, you can try using following classes. They won’t win you any awards for readability, but they allow you to use using for locking as you wanted.

    class DisposableLock
    {
        public bool IsAcquired { get; set; }
    
        class Handle : IDisposable
        {
            private DisposableLock parent;
    
            public void Dispose()
            {
                parent.IsAcquired = false;
                Monitor.Exit(parent);
            }
        }
    
        public IDisposable Acquire()
        {
            var handle = new Handle();
            handle.parent = this;
            handle.parent.IsAcquired = true;
            Monitor.Enter(this);
            return handle;
        }
    }
    

    The usage:

    DisposableLock lockForSomething = new DisposableLock();
    // ...
    
    using (lockForSomething.Acquire())
    {
    // do something
    }
    

    You can then use lockForSomething.IsAcquired in some other place to check if resource is being used. The inner class implementing IDisposable is to make sure you won’t by accident use using (lockForSomething).

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

Sidebar

Related Questions

foo\r\nbar.replace(/(foo).+/m, bar) Hello. I can not understand why this code does not replace foo
How can I use sed to replace this line char * path_list_[1] = {
I'm using a try-catch block in the following Actionscript 3 code: try { this._subtitle
I have a string like this BRADI5G20430|BRADI5G20430.1||1 How can I replace the bar (single
I have this java string: String bla = <my:string>invalid_content</my:string>; How can I replace the
How can I update this regular expression to find and replace not just /news/
MySQL has this incredibly useful yet proprietary REPLACE INTO SQL Command. Can this easily
i m just wondering if we can do this with preg replace like if
hi to all how can i make this code to wait until it finishes
I'm using phpunit framework and I have a code like this: public function A()

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.