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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:15:27+00:00 2026-05-19T17:15:27+00:00

I have multiple chunks of code similar to the following: void GetPerson(Action<PersonView, Exception> callback);

  • 0

I have multiple chunks of code similar to the following:

void GetPerson(Action<PersonView, Exception> callback);

...

IsBusy = true;
_personRequested = true;
_service.GetPerson((person, error) => 
{
 if (error != null)
  {
    return;
  }
  _person = person;
  _personLoaded = true;
  IsBusy = false;
});

The problem I am running in to is that a given class might fire off multiple different async calls, and the IsBusy property has to be ‘smart’ to not toggle off just because call ‘A’ is completed, but ‘B’ and ‘C’ are still pending. Hence the _personRequested and _personLoaded booleans. However, I’d like to go to something more generic, but am a bit unsure how to proceed.

My initial thought was to set up a function that takes in the above code as a delegate, but I keep getting bogged down by odd syntax. I’d like functionality such that I’d simply wrap the whole thing and pass it to my function as an anonymous method, then in that function and the callback I’d handle my classes’ busy states.

Any help is appreciated, 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-05-19T17:15:28+00:00Added an answer on May 19, 2026 at 5:15 pm

    If all you’re looking for is an indicator that something’s in use, you’re probably better off using a counter rather than a simple boolean flag. You can then use Interlocked.Increment and Interlocked.Decrement to modify the value in a thread-safe way.

    Declare this at the class level:

    private volatile int isBusyCounter;
    
    Interlocked.Increment(ref isBusyCounter);
    _personRequested = true;
    _service.GetPerson((person, error) => 
    {
     if (error != null)
      {
        return;
      }
      _person = person;
      _personLoaded = true;
      Interlocked.Decrement(ref isBusyCounter);
    });
    

    (The use of Interlocked is needed since you’re modifying the value on multiple threads, and this ensures that the updates don’t conflict with each other; we don’t care about order, since you’ll end up with the same value whether one happens first or the other)

    Then, rather than just checking IsBusy, check IsBusy > 0. The variable needs to be volatile so that subsequent reads of the variable within a method don’t get optimized away (in other words, if you’re checking this condition in a loop or multiple times within a given function, then we put volatile there to ensure that the value is checked each time rather than being cached locally).

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

Sidebar

Related Questions

I have a some code that gets passed a class derived from a certain
I have some .gz compressed files which is around 5-7gig uncompressed. These are flatfiles.
I have two models, Room and Image . Image is a generic model that
I have an issue that I feel many programmers can relate to... I have
When reading data in chunks of say, 1024, how do I continue to read
With the help from two previous questions, I now have a working HTML scraper
I'm using the fantastic jquery .validate plugin from Jörn Zaefferer at bassistance I'm using
I am currently running into a problem where an element is coming back from
Ok, so having given up on getting the built in .NET configuration system to
We provide the ability to submit images to a user's gallery. While the normal

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.