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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:37:56+00:00 2026-05-17T15:37:56+00:00

I have the following (simplified) asynchronous method: void Transform<X,Y>(X x, Action<Y> resultCallback) {…} and

  • 0

I have the following (simplified) asynchronous method:

void Transform<X,Y>(X x, Action<Y> resultCallback) {...}

and what I want to do is transform a list of Xs into a list of Ys.

The problem is that even though the Transform method is asynchronous, it has to be called serially (i.e. I have to wait for the callback before calling it with the next value).

Is there any way to do this elegantly? (I’m on .Net 4.0)

I’m guessing there might be some way to do it with continuation passing…

UPDATE I forgot to specify that I don’t want to block the calling (GUI) 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-17T15:37:57+00:00Added an answer on May 17, 2026 at 3:37 pm

    If you wrap this in a helper class, you could make the helper “synchronize” your values:

    public class AsyncWrapper<X,Y>
    {
        ManualResetEvent mre;
        private Y result; 
    
        public Y Transform(X x, YourClass yourClass)
        {
            mre = new ManualResetEvent(false);
            result = default(Y);
    
            yourClass.Transform<X,Y>(x, this.OnComplete);
            mre.WaitOne();
            return result;
        }
    
        void OnComplete(Y y)
        {
            result = y;
            mre.Set();
        }        
    }
    

    You could then use this like:

    // instance your class with the Transform operation
    YourClass yourClass = new YourClass();
    
    AsyncWrapper<X,Y> wrapper = new AsyncWrapper<X,Y>();
    
    foreach(X x in theXCollection)
    {
         Y result = wrapper.Transform(x, yourClass);
    
         // Do something with result
    }
    

    Edit:

    Since you say you’re trying to do this to keep everything running on a background thread, you can use my code above, and do:

    // Start "throbber"
    Task.Factory.StartNew () =>
    {
        // instance your class with the Transform operation
        YourClass yourClass = new YourClass();
    
        AsyncWrapper<X,Y> wrapper = new AsyncWrapper<X,Y>();
    
        foreach(X x in theXCollection)
        {
             Y result = wrapper.Transform(x, yourClass);
    
             // Do something with result
        }
    }).ContinueWith( t =>
    {
        // Stop Throbber
    }, TaskScheduler.FromCurrentSynchronizationContext());
    

    This will start the entire (now synchronous) process on a background thread, and disable your “throbber” (from comment) on the UI thread once it completes.

    If you control all of this code, you can make your Transform process synchronous from the start, and just move it into a background thread as above, avoiding the need for the wrapper.

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

Sidebar

Related Questions

No related questions found

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.