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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:46:21+00:00 2026-05-20T09:46:21+00:00

I am trying on RX for the first time and I have a couple

  • 0

I am trying on RX for the first time and I have a couple questions.

1) Is there a better way to accomplish the Async of my collection?

2) I need to block on the thread until all Async Tasks are complete, how do I do that?

class Program
{

    internal class MyClass
    {
        private readonly List<int> _myData = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        private readonly Random random = new Random();

        public int DoSomething(int j)
        {
            int i = random.Next(j * 1000) - (j * 200);
            i = i < 0 ? 1000 : i;
            Thread.Sleep(i);
            Console.WriteLine(j);
            return j;
        }

        public IObservable<int> DoSomethingAsync(int j)
        {
            return Observable.CreateWithDisposable<int>(
                o => Observable.ToAsync<int, int>(DoSomething)(j).Subscribe(o)
                );
        }

        public void CreateTasks()
        {
            _myData.ToObservable(Scheduler.NewThread).Subscribe(
            onNext: (i) => DoSomethingAsync(i).Subscribe(),
            onCompleted: () => Console.WriteLine("Completed")
                );
        }
    }

    static void Main(string[] args)
    {
        MyClass test = new MyClass();

        test.CreateTasks();

        Console.ReadKey(); 
    } 
}

(Note: I know I could have used Observable.Range for my list of Int but my list is not of type Int in the real program).

  • 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-20T09:46:22+00:00Added an answer on May 20, 2026 at 9:46 am

    I’d probably try

    public void CreateTasks()                
    {                       
        _myData.ToObservable(Scheduler.NewThread)
            .SelectMany(i => Observable.Start(() => DoSomething(i)))
            .Subscribe(j => Console.WriteLine("j is {0}", j), 
                      () => Console.WriteLine("Completed"));       
    } 
    

    So firstly I’ve changed the DoSomethingAsync so that it uses Observable.Start. Observable.Start will run the DoSomething method asyncronously and return the value through IObservable.OnNext when the method completes.

    Then the CreateTasks method runs over each item in the collection as it did before, but feeds each value into a SelectMany which continues with a call the DoSomethingAsync method. The result is, that you’ll then recieve an OnNext for each completed call to DoSomethingAsync and an OnComplete when they are all complete.

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

Sidebar

Related Questions

Need your help. I am trying it first time. I have used following code.
I have a problem here im trying to upload a file first time it
I am trying to implement a Custom HttpHandler (for the first time), I have
I am just trying to read .csv file first time.I have gone through the
I'm trying out IntelliJ IDEA in earnest for the first time and have installed
I am tryign to use Doctrine_Pager for the first time. So I have the
First time trying to deal with users logging in with ASP.net and haven't had
I was trying for the first time to read data from a file, a
This is my first time trying to set up a database. My intent is
this is my first time trying to use document classes in AS3 and im

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.