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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:16:16+00:00 2026-05-26T03:16:16+00:00

So I know you can use a semaphore and possibly other methods to determine

  • 0

So I know you can use a semaphore and possibly other methods to determine if a lock will succeed and/or how many threads are waiting for the lock but can you do this from within the lock itself?

My situation is I have an event handler that could be called by many objects within a List. I want to do a test within a lock to see if all the objects are in the right state before proceeding. The objects transition to the appropriate state and then raise this event so in a single thread model when the last one raises this event, all objects will be in the right state and we proceed. But it occurred to me that, with multiple threads, multiple objects may be transitioned to the state I’m checking for but as yet have not processed this event as they are waiting at the lock. Therefore the conditional state check within the lock will be true but proceeding would be bad till all threads have finished processing this event. I need it to only be true as the last thread is processed to ensure we don’t proceed too soon.

For example in semi realistic code:

object _LockObj = new object();

void Event_Handler(object sender, EventArgs e)
{
    MyObject originator = sender as MyObject;
    if(originator == null)
        return;

    *Do stuff with the originator*

    lock(_LockObj)
    {
        if(ListOfMyObjects.FindAll(o => o.State == DesiredState)
                          .Count == ListOfMyObjects.Count 
                                 && *nothing waiting at the lock*)
        {
            *Proceed*
        }
    }
}

I’m perfectly prepared to accept my approach is smelly to start with and that the solution I’m looking for is moot if I do it properly in the first place but I’m uncertain how to do it properly in a thread safe manner.

I was going to add yet more states to MyObject and manage the flow by setting the appropriate State in the *Do stuff with the originator* section but it didn’t feel correct to be transitioning MyObject from here, not to mention I then have to implement a holding state for each state that raises an event which is getting smellier not simpler!

So if there is a simple way of doing the ‘nothing waiting at the lock‘ check then that’s the way I’d like to go unless I’m missing a really simple to implement pattern that helps in this scenario.

  • 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-26T03:16:16+00:00Added an answer on May 26, 2026 at 3:16 am

    First off, unfortunately, testing for other threads waiting on the lock() statement will not get you what you want. For example, if objects A, B and C have transitioned to the desired state and have raised their events, it’s possible that the Event_Handler is only called for one object, say A at that point. The same handler will run for B and C at some point, later. So all your objects are now supposedly in the desired state, AND no threads are waiting at the lock, but you prob. don’t want to “proceed”. Even if you do, you need to consider, the same might happen an instant later for B and then again for C, so “proceed” may run three times…

    It’s a simple suggestion, but you can use a counter to test how many objects have actually raised the event. You could also use another list if a counter doesn’t give sufficient information. The important part is that whichever method you use, only update the counter / list / other mechanism while inside a lock on the same _LockObj. Then probably reset that counter after proceed has run. If you need to access the counter somewhere else, make sure you use the same lock(_LockObj), this will ensure you’re not changing it while inside the other critical section.

    object _LockObj = new object();
    int _counter = 0;
    
    void Event_Handler(object sender, EventArgs e)
    {
        MyObject originator = sender as MyObject;
        if(originator == null)
            return;
    
        *Do stuff with the originator*
    
        lock(_LockObj)
        {
            ++_counter;
            if (_counter == ListOfMyObjects.Count)
            {
                *Proceed*
    
                _counter = 0;
            }
        }
    }
    

    Edit: Removed redundant check for objects’ state.

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

Sidebar

Related Questions

I know I can use ReadKey for that but it will freeze the app
I know you can use source control software for source code, but can you
I know I can use Request.Browser.IsMobileDevice . But does anyone know how it works,
I know I can use $.data but how can I iterate trough all data-
I know you can use the Profile provider for custom fields, but given the
I know can use string.find() to find a substring in a string. But what
I know you can use @ to suppress errors. But is there anyway you
I know I can use the following methods of trafficstats api to get bytes
I know i can use AJAX and SILVERLIGHT with my ASP.NET web page. But
I know we can use *21*Phone Number# will make a call forwarding.i want to

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.