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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:22:14+00:00 2026-05-14T04:22:14+00:00

If I have an object that I would like to force to be accessed

  • 0

If I have an object that I would like to force to be accessed from within a lock, like so:

var obj = new MyObject();

lock (obj)
{
    obj.Date = DateTime.Now;
    obj.Name = "My Name";
}

Is it possible, from within the AddOne and RemoveOne functions to detect whether the current execution context is within a lock?

Something like:

Monitor.AreWeCurrentlyEnteredInto(this)

Edit: (for clarification of intent)

The intent here is to be able to reject any modification made outside of the lock, so that all changes to the object itself will be transactional and thread-safe. Locking on a mutex within the object itself does not ensure a transactional nature to the edits.


I know that it is possible to do this:

var obj = new MyObject();

obj.MonitorEnterThis();

try
{
    obj.Date = DateTime.Now;
    obj.Name = "My Name";
}
finally
{
    obj.MonitorExitThis();
}

But this would allow any other thread to call the Add/Remove functions without first calling the Enter, thereby circumventing the protection.


Edit 2:

Here is what I’m currently doing:

var obj = new MyObject();

using (var mylock = obj.Lock())
{
    obj.SetDate(DateTime.Now, mylock);
    obj.SetName("New Name", mylock);
}

Which is simple enough, but it has two problems:

  1. I’m implementing IDisposable on the
    mylock object, which is a little bit
    of an abuse of the IDisposable
    interface.

  2. I would like to change the SetDate and SetName functions to
    Properties, for clarity.

  • 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-14T04:22:15+00:00Added an answer on May 14, 2026 at 4:22 am

    There’s no documented method of checking for this kind of condition at runtime, and if there were, I’d be suspicious of any code that used it, because any code that alters its behaviour based on the call stack would be very difficult to debug.

    True ACID semantics are not trivial to implement, and I personally wouldn’t try; that’s what we have databases for, and you can use an in-memory database if you need the code to be fast/portable. If you just want forced-single-threaded semantics, that is a somewhat easier beast to tame, although as a disclaimer I should mention that in the long run you’d be better off simply providing atomic operations as opposed to trying to prevent multi-threaded access.

    Let’s suppose that you have a very good reason for wanting to do this. Here is a proof-of-concept class you could use:

    public interface ILock : IDisposable
    {
    }
    
    public class ThreadGuard
    {
        private static readonly object SlotMarker = new Object();
    
        [ThreadStatic]
        private static Dictionary<Guid, object> locks;
    
        private Guid lockID;
        private object sync = new Object();
    
        public void BeginGuardedOperation()
        {
            lock (sync)
            {
                if (lockID == Guid.Empty)
                    throw new InvalidOperationException("Guarded operation " +
                        "was blocked because no lock has been obtained.");
                object currentLock;
                Locks.TryGetValue(lockID, out currentLock);
                if (currentLock != SlotMarker)
                {
                    throw new InvalidOperationException("Guarded operation " +
                        "was blocked because the lock was obtained on a " +
                        "different thread from the calling thread.");
                }
            }
        }
    
        public ILock GetLock()
        {
            lock (sync)
            {
                if (lockID != Guid.Empty)
                    throw new InvalidOperationException("This instance is " +
                        "already locked.");
                lockID = Guid.NewGuid();
                Locks.Add(lockID, SlotMarker);
                return new ThreadGuardLock(this);
            }
        }
    
        private void ReleaseLock()
        {
            lock (sync)
            {
                if (lockID == Guid.Empty)
                    throw new InvalidOperationException("This instance cannot " +
                        "be unlocked because no lock currently exists.");
                object currentLock;
                Locks.TryGetValue(lockID, out currentLock);
                if (currentLock == SlotMarker)
                {
                    Locks.Remove(lockID);
                    lockID = Guid.Empty;
                }
                else
                    throw new InvalidOperationException("Unlock must be invoked " +
                        "from same thread that invoked Lock.");
            }
        }
    
        public bool IsLocked
        {
            get
            {
                lock (sync)
                {
                    return (lockID != Guid.Empty);
                }
            }
        }
    
        protected static Dictionary<Guid, object> Locks
        {
            get
            {
                if (locks == null)
                    locks = new Dictionary<Guid, object>();
                return locks;
            }
        }
    
        #region Lock Implementation
    
        class ThreadGuardLock : ILock
        {
            private ThreadGuard guard;
    
            public ThreadGuardLock(ThreadGuard guard)
            {
                this.guard = guard;
            }
    
            public void Dispose()
            {
                guard.ReleaseLock();
            }
        }
    
        #endregion
    }
    

    There’s a lot going on here but I’ll break it down for you:

    • Current locks (per thread) are held in a [ThreadStatic] field which provides type-safe, thread-local storage. The field is shared across instances of the ThreadGuard, but each instance uses its own key (Guid).

    • The two main operations are GetLock, which verifies that no lock has already been taken and then adds its own lock, and ReleaseLock, which verifies that the lock exists for the current thread (because remember, locks is ThreadStatic) and removes it if that condition is met, otherwise throws an exception.

    • The last operation, BeginGuardedOperation, is intended to be used by classes that own ThreadGuard instances. It’s basically an assertion of sorts, it verifies that the currently-executed thread owns whichever lock is assigned to this ThreadGuard, and throws if the condition isn’t met.

    • There’s also an ILock interface (which doesn’t do anything except derive from IDisposable), and a disposable inner ThreadGuardLock to implement it, which holds a reference to the ThreadGuard that created it and calls its ReleaseLock method when disposed. Note that ReleaseLock is private, so the ThreadGuardLock.Dispose is the only public access to the release function, which is good – we only want a single point of entry for acquisition and release.

    To use the ThreadGuard, you would include it in another class:

    public class MyGuardedClass
    {
        private int id;
        private string name;
        private ThreadGuard guard = new ThreadGuard();
    
        public MyGuardedClass()
        {
        }
    
        public ILock Lock()
        {
            return guard.GetLock();
        }
    
        public override string ToString()
        {
            return string.Format("[ID: {0}, Name: {1}]", id, name);
        }
    
        public int ID
        {
            get { return id; }
            set
            {
                guard.BeginGuardedOperation();
                id = value;
            }
        }
    
        public string Name
        {
            get { return name; }
            set
            {
                guard.BeginGuardedOperation();
                name = value;
            }
        }
    }
    

    All this does is use the BeginGuardedOperation method as an assertion, as described earlier. Note that I’m not attempting to protect read-write conflicts, only multiple-write conflicts. If you want reader-writer synchronization then you’d need to either require the same lock for reading (probably not so good), use an additional lock in MyGuardedClass (the most straightforward solution) or alter the ThreadGuard to expose and acquire a true “lock” using the Monitor class (be careful).

    And here’s a test program to play with:

    class Program
    {
        static void Main(string[] args)
        {
            MyGuardedClass c = new MyGuardedClass();
            RunTest(c, TestNoLock);
            RunTest(c, TestWithLock);
            RunTest(c, TestWithDisposedLock);
            RunTest(c, TestWithCrossThreading);
            Console.ReadLine();
        }
    
        static void RunTest(MyGuardedClass c, Action<MyGuardedClass> testAction)
        {
            try
            {
                testAction(c);
                Console.WriteLine("SUCCESS: Result = {0}", c);
            }
            catch (Exception ex)
            {
                Console.WriteLine("FAIL: {0}", ex.Message);
            }
        }
    
        static void TestNoLock(MyGuardedClass c)
        {
            c.ID = 1;
            c.Name = "Test1";
        }
    
        static void TestWithLock(MyGuardedClass c)
        {
            using (c.Lock())
            {
                c.ID = 2;
                c.Name = "Test2";
            }
        }
    
        static void TestWithDisposedLock(MyGuardedClass c)
        {
            using (c.Lock())
            {
                c.ID = 3;
            }
            c.Name = "Test3";
        }
    
        static void TestWithCrossThreading(MyGuardedClass c)
        {
            using (c.Lock())
            {
                c.ID = 4;
                c.Name = "Test4";
                ThreadPool.QueueUserWorkItem(s => RunTest(c, cc => cc.ID = 5));
                Thread.Sleep(2000);
            }
        }
    }
    

    As the code (hopefully) implies, only the TestWithLock method completely succeeds. The TestWithCrossThreading method partially succeeds – the worker thread fails, but the main thread has no trouble (which, again, is the desired behaviour here).

    This isn’t intended to be production-ready code, but it should give you the basic idea of what has to be done in order to both (a) prevent cross-thread calls and (b) allow any thread to take ownership of the object as long as nothing else is using it.

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

Sidebar

Related Questions

I have an object that I created previously. obj = new Object(); I would
Ok, NHibernate question here. I have two objects that I would like to map
I have a Queue object that I need to ensure is thread-safe. Would it
I have an object that can build itself from an XML string, and write
I have an object that has a list of abstract 'aninamls'. i.e. var animals
I have an object that contains another object with translations. It looks like this:
I have an object that is mapped to a cookie as a serialized base-64
I have an object that implements IDisposable that is registered with the Windsor Container
I have an object that needs a test if the object data is valid.
I have an object that starts a thread, opens a file, and waits for

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.