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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:53:17+00:00 2026-06-17T06:53:17+00:00

I have a connection class which has several async methods such as SendText, SendImage

  • 0

I have a connection class which has several async methods such as SendText, SendImage etc.

The connection class has a Disconnect method, and when it is called I have to be careful not to start changing the inner state of the class before all async methods have completed execution.

I believe a good way to achieve this is to simply keep a running total of the number of operations in execution, and then when I want to Disconnect I can simply set Disconnecting = true and then wait for the count to reach 0

I’m thinking of something along the lines of this

class ReferenceCounter
{
    void Increment();

    void Decrement();

    async Task WaitForCounterToReachZero();
}

Then when an async operation starts I could do

refCounter.Increment();

When it ends

refCounter.Decrement();

and inside the Disconnect method

disconnecting = true;
taskCancellationSource.Cancel();
await refCounter.WaitForCounterToReachZero();
Cleanup();

Are there any built in .NET classes like this?

Or more importantly for me, is there a better way of doing this?

If it was synchronous code it would be as simple as

lock (thisLock)
{
    while (counter > 0)
        Monitor.Wait(thisLock);
}

I just found the built in CountdownEvent class which does the same thing, but it has no async Wait method nor does it have any events, so I’d have to block.

  • 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-17T06:53:18+00:00Added an answer on June 17, 2026 at 6:53 am

    Well, assuming you’ll never increment again after you make it 0, you could do something like this:

    public class Latch
    {
        private int count = 0;
        private readonly TaskCompletionSource<object> tcs =
            new TaskCompletionSource<object>();
    
        public void Increment()
        {
            Interlocked.Increment(ref count);
        }
    
        public void Decrement()
        {
            if (Interlocked.Decrement(ref count) == 0)
            {
                tcs.TrySetValue(null);
            }
        }
    
        public Task Task { get { return tcs.Task; } }
    }
    

    Then you can await someLatch.Task. Alternatively, you could make the latch itself awaitable:

    public TaskAwaiter GetAwaiter()
    {
        return tcs.Task.GetAwaiter();
    }
    

    You should probably consider how you want to guard against the “count rises after getting down to 0” aspect thuogh – think about what you’d want it to do. (In the code above, once the TCS’s value has been set, further awaits will complete immediately.)

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

Sidebar

Related Questions

I have a TcpDevice class which encapsulates a TCP connection, which has an onRemoteDisconnect
I have a Person class which has a String collection of aliases representing additional
I have an APVendor class, which has a collection of APInvoice classes and a
I have a base class ReportElement which has type property: public abstract class ReportElement
I have a class called Contacts which contains a collection of objects called PhoneNumbers
I have a Rails app which has 2 databases. Legacy DB with table called
I have a Connection class which reads packets (commands) from a text stream and
We have a class library (ProjA) which has L2E object context. This will be
I now have an event processor class which has a Dictionary to store all
I have a Question class that has public List property that can contain several

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.