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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:40:58+00:00 2026-06-04T22:40:58+00:00

I have a worker which exposes a Subject<string> , which publishes log messages very

  • 0

I have a worker which exposes a Subject<string>, which publishes log messages very quickly. Writing to the console is slow, so I want to only write to the console every 100ms, at the most. When the task is finished I want to write out the most recent published string, to avoid having things like Doing work 2312/2400 ...done. (or even ...done if the task takes <100ms.)

I’m new to reactive extensions, and though I’ve heard talks about how awesome they are, this is the first time I’ve noticed a situation where they could help me.

So, in summary,
1) Don’t give me an event more than once every 100ms
2) I need to know about the final event, regardless of when it arrives.

I’ll put my code in an answer below, but please suggest something better. Maybe I’ve missed a standard call which achieves this?

  • 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-04T22:40:59+00:00Added an answer on June 4, 2026 at 10:40 pm

    It’s actually simpler than you make out if the observable that is the source of your events called OnCompleted() when it’s done. This will do the trick by itself:

    observable.Sample(TimeSpan.FromMilliseconds(100)).Subscribe(log);
    

    This is because Sample will fire one last time when the source of it’s events completes. Here’s a full test:

    var sub = new Subject<string>();
    var gen = Observable.Interval(TimeSpan.FromMilliseconds(50)).Select((_,i) => i).Subscribe(i => sub.OnNext(i.ToString()));
    
    sub.Sample(TimeSpan.FromSeconds(1))
       .Subscribe(Console.WriteLine);
    
    Thread.Sleep(3500);
    sub.OnCompleted();
    

    Even though I sleep for 3.5 seconds, 4 events fire, the last one firing when I call OnCompleted().

    Another point to note is that it’s bad form if Worker.GetObservable() actually returns a Subject<string> – even if it is that in the Worker class, what it really should do is return just the IObservable<string> interface. This is more than mere style, it is a separation of concerns and returning the minimum functional interface needed.

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

Sidebar

Related Questions

I have a MFC application, which has a worker thread, what I want to
I have a Worker Role which isn´t working on Cloud environment. When I run
I have a background worker which can be cancelled. The normal flows interrupt itself
I have a WinForms application in which my background worker is doing a sync
I have a worker QThread in which a QObject derived object is created. The
I have a worker class which establishes a Two data bindings of some UI
I am a little confused. I have a background worker which in its dowork
I have a 'worker' process which I am going to assign to a job.
I have a background worker which needs to grab comboBox1.SelectedItem , however it's on
I have a background worker which updates the GUI on a regular basis via

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.