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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:33:38+00:00 2026-05-15T20:33:38+00:00

I want to effectively throttle an event stream, so that my delegate is called

  • 0

I want to effectively throttle an event stream, so that my delegate is called when the first event is received but then not for 1 second if subsequent events are received. After expiry of that timeout (1 second), if a subsequent event was received I want my delegate to be called.

Is there a simple way to do this using Reactive Extensions?

Sample code:

static void Main(string[] args)
{
    Console.WriteLine("Running...");

    var generator = Observable
        .GenerateWithTime(1, x => x <= 100, x => x, x => TimeSpan.FromMilliseconds(1), x => x + 1)
        .Timestamp();

    var builder = new StringBuilder();

    generator
        .Sample(TimeSpan.FromSeconds(1))
        .Finally(() => Console.WriteLine(builder.ToString()))
        .Subscribe(feed =>
                   builder.AppendLine(string.Format("Observed {0:000}, generated at {1}, observed at {2}",
                                                    feed.Value,
                                                    feed.Timestamp.ToString("mm:ss.fff"),
                                                    DateTime.Now.ToString("mm:ss.fff"))));

    Console.ReadKey();
}

Current output:

Running...
Observed 064, generated at 41:43.602, observed at 41:43.602
Observed 100, generated at 41:44.165, observed at 41:44.602

But I want to observe (timestamps obviously will change)

Running...
Observed 001, generated at 41:43.602, observed at 41:43.602
....
Observed 100, generated at 41:44.165, observed at 41:44.602
  • 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-15T20:33:38+00:00Added an answer on May 15, 2026 at 8:33 pm

    Here’s is what I got with some help from the RX Forum:

    The idea is to issue a series of “tickets” for the original sequence to fire. These “tickets” are delayed for the timeout, excluding the very first one, which is immediately pre-pended to the ticket sequence. When an event comes in and there is a ticket waiting, the event fires immediately, otherwise it waits till the ticket and then fires. When it fires, the next ticket is issued, and so on…

    To combine the tickets and original events, we need a combinator. Unfortunately, the “standard” .CombineLatest cannot be used here because it would fire on tickets and events that were used previousely. So I had to create my own combinator, which is basically a filtered .CombineLatest, that fires only when both elements in the combination are “fresh” – were never returned before. I call it .CombineVeryLatest aka .BrokenZip 😉

    Using .CombineVeryLatest, the above idea can be implemented as such:

        public static IObservable<T> SampleResponsive<T>(
            this IObservable<T> source, TimeSpan delay)
        {
            return source.Publish(src =>
            {
                var fire = new Subject<T>();
    
                var whenCanFire = fire
                    .Select(u => new Unit())
                    .Delay(delay)
                    .StartWith(new Unit());
    
                var subscription = src
                    .CombineVeryLatest(whenCanFire, (x, flag) => x)
                    .Subscribe(fire);
    
                return fire.Finally(subscription.Dispose);
            });
        }
    
        public static IObservable<TResult> CombineVeryLatest
            <TLeft, TRight, TResult>(this IObservable<TLeft> leftSource,
            IObservable<TRight> rightSource, Func<TLeft, TRight, TResult> selector)
        {
            var ls = leftSource.Select(x => new Used<TLeft>(x));
            var rs = rightSource.Select(x => new Used<TRight>(x));
            var cmb = ls.CombineLatest(rs, (x, y) => new { x, y });
            var fltCmb = cmb
                .Where(a => !(a.x.IsUsed || a.y.IsUsed))
                .Do(a => { a.x.IsUsed = true; a.y.IsUsed = true; });
            return fltCmb.Select(a => selector(a.x.Value, a.y.Value));
        }
    
        private class Used<T>
        {
            internal T Value { get; private set; }
            internal bool IsUsed { get; set; }
    
            internal Used(T value)
            {
                Value = value;
            }
        }
    

    Edit: here’s another more compact variation of CombineVeryLatest proposed by Andreas Köpf on the forum:

    public static IObservable<TResult> CombineVeryLatest
      <TLeft, TRight, TResult>(this IObservable<TLeft> leftSource,
      IObservable<TRight> rightSource, Func<TLeft, TRight, TResult> selector)
    {
        return Observable.Defer(() =>
        {
            int l = -1, r = -1;
            return Observable.CombineLatest(
                leftSource.Select(Tuple.Create<TLeft, int>),
                rightSource.Select(Tuple.Create<TRight, int>),
                    (x, y) => new { x, y })
                .Where(t => t.x.Item2 != l && t.y.Item2 != r)
                .Do(t => { l = t.x.Item2; r = t.y.Item2; })
                .Select(t => selector(t.x.Item1, t.y.Item1));
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that is effectively a replacement for print, and I want
I have a build script calling xcodebuild. that works, but I want to also
Effectively I want to give numeric scores to alphabetic grades and sum them. In
I want to reference a COM DLL in a .NET project, but I also
I effectively want two nodes: Normal Node Premium Node The only difference will be
I want to select the topmost element in a document that has a given
Ok I admit, what I need is not a tabpanel, (or is it?). But
I want to be able to quickly deploy updates to a site that is
I have a header that I want to stick to the top and a
I want to use a temp directory that will be unique to this build.

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.