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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:55:47+00:00 2026-06-05T15:55:47+00:00

In an effort to lean Rx I’m writing a simple application which imitates reading

  • 0

In an effort to lean Rx I’m writing a simple application which imitates reading a file and supports cancellation.

It works like this:

  • When the user presses the “Run” button the program

    • shows the busy indicator,
    • reads a file in chunks, which is a long process, reporting the length of each one,
    • hides the busy indicator when the reading process is finished (or canceled, see below).
  • If the user presses the “Cancel” button the file reading process gets canceled.

  • If the user presses the “Run” button while a previous operation is still in progress, the previous operation gets canceled and a new one starts.

I’m looking for a way to implement this with minimum glue code while still maintaining the look of a simple sequence of synchronous operations in which the natural control flow is easily seen.

The main problem seems to be hiding the busy indicator when the inner observable (the reader) finishes, the outer observable being the “Run” click event sequence.

So far I came up with these two versions of the main piece of the program:

subscriber-based:

IObservable<byte[]> xs =
   runClick
   .Do((Action<EventPattern<EventArgs>>)(_ => ShowBusy(true)))
   .Do(_ => ShowError(false))
   .Select(_ =>
      reader
      .TakeUntil(cancelClick)
      .Publish(ob =>
      {
         ob.Subscribe(
            b => { },
            ex =>
            {
               Console.WriteLine("ERROR: " + ex);
               ShowBusy(false);
               ShowError(true);
            },
            () => ShowBusy(false));
         return ob;
      }))
   .Switch();
xs = xs.Catch<byte[], Exception>(e => xs);

and concat-based:

IObservable<byte[]> xs =
   runClick
   .Do((Action<EventPattern<EventArgs>>)(_ => ShowBusy(true)))
   .Do(_ => ShowError(false))
   .Select(_ =>
      reader
      .TakeUntil(cancelClick)
      .DoAfter(() =>
         ShowBusy(false)))
   .Switch();
xs = xs.Catch<byte[], Exception>(e =>
{
   Console.WriteLine("ERROR: " + e);
   ShowBusy(false);
   ShowError(true);
   return xs;
});

with one custom method DoAfter

public static IObservable<T> DoAfter<T>(this IObservable<T> observable, Action action)
{
   IObservable<T> appended = observable.Concat(
      Observable.Create<T>(o =>
      {
         try
         {
            action();
         }
         catch (Exception ex)
         {
            o.OnError(ex);
            return Disposable.Empty;
         }

         o.OnCompleted();
         return Disposable.Empty;
      }));
   return appended;
}

(see the whole application code here).

Neither implementation satisfies me completely: the first is rather verbose, the second forces me to write my own method for a rather simple task which I would expect to be covered by standard means. Am I missing something?

  • 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-05T15:55:48+00:00Added an answer on June 5, 2026 at 3:55 pm

    It turned out what I was missing was an overload of the Do method. I can use it instead of my custom DoAfter method and then the code looks good.

    IObservable<byte[]> xs =
       runClick
       .Do((Action<EventPattern<EventArgs>>)(_ => ShowBusy(true)))
       .Do(_ => ShowError(false))
       .Select(_ =>
          reader
          .TakeUntil(cancelClick)
          .Do(
             b => { },
             ex =>
             {
                Console.WriteLine("ERROR: " + ex);
                ShowBusy(false);
                ShowError(true);
             },
             () => ShowBusy(false)
          ))
       .Switch();
    xs = xs.Catch<byte[], Exception>(e => xs);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is an effort to develop a Android application which enable C2DM Things I
This is my first effort on solving the exercise. I gotta say, I'm kind
In an effort to 'clean up' my code - I was wondering if this
In an effort to create a sandbox for CoreGraphics development (which currently consists of
In an effort to learn more about networking I'd like to do an exercise:
In an effort to further refine my question this is the 3rd rewrite. Here
The below text is an effort to expand and add color to this question:
Effort I've read this question , but I still think there has to be
In an effort to avoid auto sequence numbers and the like for one reason
I'm diving into CSS in an effort to be more well-rounded. I'm currently reading

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.