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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:22:24+00:00 2026-05-16T07:22:24+00:00

I have some calls that must execute sequentially. Consider an IService that has a

  • 0

I have some calls that must execute sequentially. Consider an IService that has a Query and a Load method. The Query gives a list of widgets, and the load provides a “default” widget. Hence, my service looks like this.

void IService.Query(Action<IEnumerable<Widget>,Exception> callback);
void IService.Load(Action<Widget,Exception> callback); 

With that in mind, here is a rough sketch of the view model:

public class ViewModel : BaseViewModel
{
   public ViewModel()
   {
      Widgets = new ObservableCollection<Widget>();

      WidgetService.Query((widgets,exception) =>
      {
          if (exception != null) 
          {
              throw exception;
          }

          Widgets.Clear();

          foreach(var widget in widgets)
          {
             Widgets.Add(widget);
          }

          WidgetService.Load((defaultWidget,ex) =>
          {
             if (ex != null)
             {
                 throw ex;
             }
             if (defaultWidget != null)
             {
                CurrentWidget = defaultWidget;
             }
          }
      });
   }

   public IService WidgetService { get; set; } // assume this is wired up

   public ObservableCollection<Widget> Widgets { get; private set; }

   private Widget _currentWidget; 

   public Widget CurrentWidget 
   {
      get { return _currentWidget; }
      set 
      {
         _currentWidget = value; 
         RaisePropertyChanged(()=>CurrentWidget);
      }
   }
}

What I’d like to do is simplify the sequential workflow of calling query and then the default. Perhaps the best way to do this is nested with lambda expressions as I’ve shown, but I figured there may be a more elegant way with Rx. I don’t want to use Rx for the sake of Rx, but if it can allow me to organize the logic above so it is easier to read/maintain in the method, I’ll take advantage of it. Ideally, something like:

Observable.Create(
   ()=>firstAction(), 
   ()=>secondAction())
.Subscribe(action=>action(),error=>{ throw error; }); 

With the power threading library, I’d do something like:

Service.Query(list=>{result=list};
yield return 1;
ProcessList(result);
Service.Query(widget=>{defaultWidget=widget};
yield return 1;
CurrentWidget = defaultWidget;

That makes it far more evident that the workflow is sequential and eliminates nesting (the yields are part of the async enumerator and are boundaries that block until the results come back).

Anything similar would make sense to me.

So the essence of the question: am I trying to fit a square peg into a round hole, or is there a way to redefine the nested asynchronous calls using Rx?

  • 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-16T07:22:25+00:00Added an answer on May 16, 2026 at 7:22 am

    You can convert methods of service so they return IObservable instead of taking callback as a parameter. In this case sequential workflow can be implemented using SelectMany, something like this…

            WidgetService.Query()
                .SelectMany(
                    widgets =>
                    {
                        Widgets.Clear();
                        foreach (var w in widgets)
                        {
                            Widgets.Add(w);
                        }
    
                        return WidgetService.Load();
                    }
                )
                .Do(
                    defaultWidget =>
                    {
                        if (defaultWidget != null)
                            Default = defaultWidget;
                    }
                )
                .Subscribe(
                    _ => { },
                    e => { throw e; }
                );
    

    However IMO F# asyncs will look much more clear (in sample I assume that methods of service returns Async> and Async respectively). Note that sample doesn’t take in account what thread is modifying data fields, in real-world code you should pay attention to this:

        let load = async {
                let! widgets = WidgetService.Query()
    
                Widgets.Clear()
                for w in widgets do
                    Widgets.Add(w)
    
                let! defaultWidget = WidgetService.Load()
                if defaultWidget <> null then
                    Default <- defaultWidget
    
                return ()
            }
    
        Async.StartWithContinuations(
            load, 
            ignore, // success continuation - ignore result
            raise,  // error continuation - reraise exception
            ignore  // cancellation continuation - ignore
            )
    

    EDITED

    In fact it is possible to use technique with iterators you mentioned in your question:

        private IEnumerable<IObservable<object>> Intialize()
        {
            var widgetsList = WidgetService.Query().Start();
            yield return widgetsList;
    
            Widgets.Clear();
            foreach (var w in widgetsList[0])
            {
                Widgets.Add(w);
            }
    
            var defaultWidgetList = WidgetService.Load().Start();
            yield return defaultWidgetList;
    
            if (defaultWidgetList[0] != null)
                Default = defaultWidgetList[0];
        }
    
        Observable
            .Iterate(Intialize)
            .Subscribe(
            _ => { },
            ex => { throw ex; }
            );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.