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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:11:41+00:00 2026-05-29T15:11:41+00:00

I want to start some new threads each for one repeating operation. But when

  • 0

I want to start some new threads each for one repeating operation. But when such an operation is already in progress, I want to discard the current task. In my scenario I need very current data only – dropped data is not an issue.

In the MSDN I found the Mutex class but as I understand it, it waits for its turn, blocking the current thread. Also I want to ask you: Does something exist in the .NET framework already, that does the following:

  1. Is some method M already being executed?
  2. If so, return (and let me increase some counter for statistics)
  3. If not, start method M in a new thread
  • 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-29T15:11:41+00:00Added an answer on May 29, 2026 at 3:11 pm

    The lock(someObject) statement, which you may have come across, is syntactic sugar around Monitor.Enter and Monitor.Exit.

    However, if you use the monitor in this more verbose way, you can also use Monitor.TryEnter which allows you to check if you’ll be able to get the lock – hence checking if someone else already has it and is executing code.

    So instead of this:

    var lockObject = new object(); 
    
    lock(lockObject)
    {
        // do some stuff
    }
    

    try this (option 1):

    int _alreadyBeingExecutedCounter;
    var lockObject = new object();
    
    if (Monitor.TryEnter(lockObject))
    {
       // you'll only end up here if you got the lock when you tried to get it - otherwise you'll never execute this code.
    
        // do some stuff
    
        //call exit to release the lock
        Monitor.Exit(lockObject);
    }
    else
    {
        // didn't get the lock - someone else was executing the code above - so I don't need to do any work!
       Interlocked.Increment(ref _alreadyBeingExecutedCounter);
    }
    

    (you’ll probably want to put a try..finally in there to ensure the lock is released)

    or dispense with the explicit lock althogether and do this

    (option 2)

    private int _inUseCount;
    
    public void MyMethod()
    {
        if (Interlocked.Increment(ref _inUseCount) == 1)
        {
            // do dome stuff    
        }
        Interlocked.Decrement(ref _inUseCount);
    }
    

    [Edit: in response to your question about this]

    No – don’t use this to lock on. Create a privately scoped object to act as your lock.

    Otherwise you have this potential problem:

    public class MyClassWithLockInside
    {
        public void MethodThatTakesLock()
        {
            lock(this)
            {
                // do some work
            }
        }
     }
    
    public class Consumer
    {
        private static MyClassWithLockInside _instance = new MyClassWithLockInside();
    
        public void ThreadACallsThis()
        {
              lock(_instance)
              {
                  // Having taken a lock on our instance of MyClassWithLockInside,
                  // do something long running
                  Thread.Sleep(6000);
               }
        }
    
        public void ThreadBCallsThis()
        {
             // If thread B calls this while thread A is still inside the lock above,
             // this method will block as it tries to get a lock on the same object
             // ["this" inside the class = _instance outside]
             _instance.MethodThatTakesLock();
        }  
    }
    

    In the above example, some external code has managed to disrupt the internal locking of our class just by taking out a lock on something that was externally accessible.

    Much better to create a private object that you control, and that no-one outside your class has access to, to avoid these sort of problems; this includes not using this or the type itself typeof(MyClassWithLockInside) for locking.

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

Sidebar

Related Questions

I have a page where when some operation goes wrong i want to start
I'm just learning how to do things, and want to start using some sort
After spending some time wireframing my ideas, I want to start building my rails
I'm a total newbie and want to start with php. I know some javascript
I want to start using Python for small projects but the fact that a
I want to start working with TDD but I don't know really where to
I got a fixed number of threads. I want each thread to run three
I am a novice in Threads, but I want to know how to order
I have a list of smart pointers. I want some of these smart pointers
I want to start developing for Windows Mobile Devices, as I plan to buy

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.