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 9045257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:14:42+00:00 2026-06-16T11:14:42+00:00

In system we have methods that lock an object by specific params. As implementation,

  • 0

In system we have methods that lock an object by specific params.
As implementation, we have LockManager with Enter method that receives a key for a lock, check if lock object exists in internal dictionary, if not, it creates it and then locks.

What I want to do, is to set “X expected time” for specific lock, and if an object was locked for more that X time, I want to write a message to our log.

Below is a source code for my lock manager:

public class MyLockManager<T>
{
    protected Dictionary<T, object> LockDictionary { get; private set; }

    public MyLockManager()
    {
        LockDictionary = new Dictionary<T, object>();
    }

    /// <summary>
    /// Enters a lock on the key.
    /// </summary>
    /// <param name="key">The key to lock.</param>
    public virtual void Enter(T key)
    {
        if (!LockDictionary.ContainsKey(key))
        {
            lock (LockDictionary)
            {
                if (!LockDictionary.ContainsKey(key))
                {
                    LockDictionary.Add(key, new object());
                }
            }
        }

        object lockObj = LockDictionary[key];
        Monitor.Enter(lockObj);
    }

    /// <summary>
    /// Releases the lock on the key.
    /// </summary>
    /// <param name="key">The key to release.</param>
    public virtual void Exit(T key)
    {
        if (LockDictionary.ContainsKey(key))
        {
            Monitor.Exit(LockDictionary[key]);
        }
    }
}

Now I want to add an additional method, lets say LockTimoutHandler(T key) that will be called, if object for specific key was locked for more than X time.

In order to do it, I want to add some logic to “Enter” and “Exit” methods. When Enter called, something will be somehow registered to run the LockTimoutHandler in X time, and when “Exit” called, that something will be somehow unregistered.

My question is what I can use instead of that something? How I can schedule the method to run in X time and if Exit occurred before, so remove the schedule. It must be very fast, since performance is very important in our case. I know about Timer object…it can execute method in delayed manner, but is it’s performance good enough? What additional options I have to achieve that?

NOTE: Just to be clear, I am not talking about TryEnter. I am not trying to catch the case when the object can not be locked for specific amount of time, I want to catch objects that already locked for too much time.

Thanks!

  • 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-06-16T11:14:42+00:00Added an answer on June 16, 2026 at 11:14 am

    We had a similar rquirement and solved it this way:

    • When locking, set a timer to the timeout passing it a state object, that contains the key and a delegate whatever you desire: Logging, force-unlock, … whatever is needed for your use case
    • When the timer fires, check for the key, if entry exists call delegate
    • Important: recycle the timer (e.q. in a threadsafe queue), do not let it go out of scope.
    • When you next need a timer, take one out of the recycle queue and manipulate the state object – only create a new one if you need to.

    This will keep as many timers around as necessary, but no more and will incur the cost of allocation/deallocation only once. Since you can’t change the timer’s state object, you need to change its contents.

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

Sidebar

Related Questions

I have an interface that contains a single method called ListAll() using System.Collections.Generic; namespace
I have the following method : while (TryCount < 2) { Lock.Try<object>(inCommandObj.Connection, _timeout, delegate(DataSet
I have a short lock guarded section in a method (that serves the request
I read that every object in java have a lock.That will be acquired by
I have a class in a highly concurrent system. A method getResolvedClassName() of that
Suppose I have a method like so: public byte[] GetThoseBytes() { using (System.IO.MemoryStream ms
I have a list of System.DirectoryServices.ActiveDirectory.Domain objects gotten via this method. How do I
On the class level, I have created reference: System::Data::Odbc::OdbcConnection Connection; in some method I
We have system here that uses Java JNI to call a function in a
For a newsroom system I have a class that contains a single news story.

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.