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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:01:53+00:00 2026-05-25T10:01:53+00:00

I have a library that uses the BeginXxx EndXxx asynchronous pattern (obviously, the following

  • 0

I have a library that uses the BeginXxx EndXxx asynchronous pattern (obviously, the following code is simplified):

ILibrary
{
    IAsyncResult BeginAction(string name, AsyncCallback callback, object state);
    int EndAction(IAsyncResult asyncResult, out object state)
}

I’m trying to build a class that uses that library, and that implements the same pattern itself:

Manager
{
    private ILibrary library;

    public IAsyncResult BeginMultipleActions(IEnumerable<string> names, 
                                             AsyncCallback callback, object state)
    {
        var handles = new List<IAsyncResult>();
        foreach(string name in names)
        {
            var handle = library.BeginAction(name, OnSingleActionCompleted, null);
            handles.Add(handle);
        }
        //???
    }
    public int EndMultipleActions(IAsyncResult asyncResult, out object state)
    {
        //???
    }

    private void OnSingleActionCompleted(IAsyncResult asyncResult)
    {
        //???
    }
}

I’ve tried a couple of solutions (using ManualResetEvent) but I find my code very ugly. What is the cleanest way to do this (in C#3.5) without losing functionality (callback, wait handle, …)?

  • 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-25T10:01:53+00:00Added an answer on May 25, 2026 at 10:01 am

    To be able to successfully implement the pattern you are describing, you should be able to call the callback passed into your BeginMultipleActions method after all single actions are complete. Using events and waiting on them, might defeat your sole purpose of doing async calls as they would block the threads. The callback you pass into the single action should be able to figure out when all the methods are completed and then call the callback supplied by your client to the BeginMultipleActions method. Here’s a sample that might do it for you (I haven’t tested it). However, this is only to get you started and is far from perfect. There will be several nuances that you will have to work through.

    interface ILibrary
    {
        IAsyncResult BeginAction(string name, AsyncCallback callback, object state);
        int EndAction(IAsyncResult asyncResult, out object state);
    }
    
    class Manager
    {
        private ILibrary library;
    
        class AsyncCallInfo : IAsyncResult
        {
            public int PendingOps;
            public AsyncCallback Callback { get; set; }
            public object AsyncState { get; set; }
    
            public WaitHandle AsyncWaitHandle
            {
                // Implement this if needed
                get { throw new NotImplementedException(); }
            }
    
            public bool CompletedSynchronously
            {
                get { return false; }
            }
    
            public bool IsCompleted
            {
                get { return PendingOps == 0; }
            }
        }
    
        public IAsyncResult BeginMultipleActions(IEnumerable<string> names,
                                                 AsyncCallback callback, object state)
        {
            var callInfo = new AsyncCallInfo {
                Callback = callback,
                AsyncState = state
            };
    
            callInfo.PendingOps = names.Count();
            foreach (string name in names)
            {
                library.BeginAction(name, ar => OnSingleActionCompleted(ar, callInfo), null);
            }
    
            return callInfo;
        }
    
        public int EndMultipleActions(IAsyncResult asyncResult, out object state)
        {
            // do your stuff
            state = asyncResult.AsyncState;
            return 0;
        }
    
        private void OnSingleActionCompleted(IAsyncResult asyncResult, AsyncCallInfo callInfo)
        {
            //???
            Interlocked.Decrement(ref callInfo.PendingOps);
            if (callInfo.PendingOps == 0)
            {
                callInfo.Callback(callInfo);
            }
        }
    }
    

    I suggest you take a look at Jeffrey Richter’s AsyncEnumerator. It simplifies writing asynchronous code and supports several patterns of async implementations – http://msdn.microsoft.com/en-us/magazine/cc546608.aspx

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

Sidebar

Related Questions

I have library code that uses ICSharpCode.SharpZipLib under the hood to make it easy
I have a section of code that uses a Boost library that results in
I have an Android java library that uses some native code. I have added
Common scenario: I have a library that uses other libraries. For example, a math
I have a static library (let's call it S) that uses a category (NSData+Base64
I have a third party library that internally constructs and uses the SqlConnection class.
I have a program that uses pthread library to do the matrix multiplication of
I have a project that uses the Enterprise Library 4.1. When I target .net
I have a web application that uses a library which resides in TOMCAT_HOME/common/lib. This
I have an ASP.NET application that uses a custom .NET library (a .DLL file).

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.