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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:17:00+00:00 2026-06-14T17:17:00+00:00

I´m writing a C# (.NET 4.5) application that is used to aggregate time based

  • 0

I´m writing a C# (.NET 4.5) application that is used to aggregate time based events for reporting purposes. To make my query logic reusable for both realtime and historical data I make use of the Reactive Extensions (2.0) and their IScheduler infrastructure (HistoricalScheduler and friends).

For example, assume we create a list of events (sorted chronologically, but they may coincide!) whose only payload ist their timestamp and want to know their distribution across buffers of a fixed duration:

const int num = 100000;
const int dist = 10;

var events = new List<DateTimeOffset>();
var curr = DateTimeOffset.Now;
var gap = new Random();

var time = new HistoricalScheduler(curr);

for (int i = 0; i < num; i++)
{
    events.Add(curr);
    curr += TimeSpan.FromMilliseconds(gap.Next(dist));
}

var stream = Observable.Generate<int, DateTimeOffset>(
    0,
    s => s < events.Count,
    s => s + 1,
    s => events[s],
    s => events[s],
    time);

stream.Buffer(TimeSpan.FromMilliseconds(num), time)
    .Subscribe(l => Console.WriteLine(time.Now + ": " + l.Count));

time.AdvanceBy(TimeSpan.FromMilliseconds(num * dist));

Running this code results in a System.StackOverflowException with the following stack trace (it´s the last 3 lines all the way down):

mscorlib.dll!System.Threading.Interlocked.Exchange<System.IDisposable>(ref System.IDisposable location1, System.IDisposable value) + 0x3d bytes    
System.Reactive.Core.dll!System.Reactive.Disposables.SingleAssignmentDisposable.Dispose() + 0x37 bytes    
System.Reactive.Core.dll!System.Reactive.Concurrency.ScheduledItem<System.DateTimeOffset>.Cancel() + 0x23 bytes    
...
System.Reactive.Core.dll!System.Reactive.Disposables.AnonymousDisposable.Dispose() + 0x4d bytes    
System.Reactive.Core.dll!System.Reactive.Disposables.SingleAssignmentDisposable.Dispose() + 0x4f bytes    
System.Reactive.Core.dll!System.Reactive.Concurrency.ScheduledItem<System.DateTimeOffset>.Cancel() + 0x23 bytes    
...

Ok, the problem seems to come from my use of Observable.Generate(), depending on the list size (num) and regardless of the choice of scheduler.

What am I doing wrong? Or more generally, what would be the preferred way to create an IObservable from an IEnumerable of events that provide their own timestamps?

  • 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-14T17:17:01+00:00Added an answer on June 14, 2026 at 5:17 pm

    (update – realized I didn’t provide an alternative: see at bottom of answer)

    The problem is in how Observable.Generate works – it’s used to unfold a corecursive (think recursion turned inside out) generator based on the arguments; if those arguments end up generating a very nested corecursive generator, you’ll blow your stack.

    From this point on, I’m speculating a lot (don’t have the Rx source in front of me) (see below), but I’m willing to bet your definition ends up expanding into something like:

    initial_state =>
    generate_next(initial_state) => 
    generate_next(generate_next(initial_state)) => 
    generate_next(generate_next(generate_next(initial_state))) =>
    generate_next(generate_next(generate_next(generate_next(initial_state)))) => ...
    

    And on and on until your call stack gets big enough to overflow. At, say, a method signature + your int counter, that’d be something like 8-16 bytes per recursive call (more depending on how the state machine generator is implemented), so 60,000 sounds about right (1M / 16 ~ 62500 maximum depth)

    EDIT: Pulled up the source – confirmed: the “Run” method of Generate looks like this – take note of the nested calls to Generate:

    protected override IDisposable Run(
        IObserver<TResult> observer, 
        IDisposable cancel, 
        Action<IDisposable> setSink)
    {
        if (this._timeSelectorA != null)
        {
            Generate<TState, TResult>.α α = 
                    new Generate<TState, TResult>.α(
                         (Generate<TState, TResult>) this, 
                         observer, 
                         cancel);
            setSink(α);
            return α.Run();
        }
        if (this._timeSelectorR != null)
        {
            Generate<TState, TResult>.δ δ = 
                   new Generate<TState, TResult>.δ(
                       (Generate<TState, TResult>) this, 
                       observer, 
                       cancel);
            setSink(δ);
            return δ.Run();
        }
        Generate<TState, TResult>._ _ = 
                 new Generate<TState, TResult>._(
                      (Generate<TState, TResult>) this, 
                      observer, 
                      cancel);
        setSink(_);
        return _.Run();
    }
    

    EDIT: Derp, didn’t offer any alternatives…here’s one that might work:

    (EDIT: fixed Enumerable.Range, so stream size won´t be multiplied by chunkSize)

    const int num = 160000;
    const int dist = 10;
    
    var events = new List<DateTimeOffset>();
    var curr = DateTimeOffset.Now;
    var gap = new Random();
    var time = new HistoricalScheduler(curr);
    
    for (int i = 0; i < num; i++)
    {
        events.Add(curr);
        curr += TimeSpan.FromMilliseconds(gap.Next(dist));
    }
    
        // Size too big? Fine, we'll chunk it up!
    const int chunkSize = 10000;
    var numberOfChunks = events.Count / chunkSize;
    
        // Generate a whole mess of streams based on start/end indices
    var streams = 
        from chunkIndex in Enumerable.Range(0, (int)Math.Ceiling((double)events.Count / chunkSize) - 1)
        let startIdx = chunkIndex * chunkSize
        let endIdx = Math.Min(events.Count, startIdx + chunkSize)
        select Observable.Generate<int, DateTimeOffset>(
            startIdx,
            s => s < endIdx,
            s => s + 1,
            s => events[s],
            s => events[s],
            time);
    
        // E pluribus streamum
    var stream = Observable.Concat(streams);
    
    stream.Buffer(TimeSpan.FromMilliseconds(num), time)
        .Subscribe(l => Console.WriteLine(time.Now + ": " + l.Count));
    
    time.AdvanceBy(TimeSpan.FromMilliseconds(num * dist));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a query that will be used in a .NET application, therefore
I am writing a query that will be used in a .NET application, therefore
I am writing an ASP.NET application that will keep track of bandwidth being used
I'm writing a .NET assembly in C++/CLI to be used in our C#-based application.
I'm writing a .NET Windows forms application that is to be used for importing
I am writing a .NET WinForms application that needs to display a list of
I'm writing a small .NET application that can produce SWF files, and I need
I'm writing an ASP.net application that uses Windows Identity Foundation. My ASP.net application uses
I'm writing a .NET command-line application that will migrate users from an existing database
I currently have an application I'm writing in c# (using .NET) that requires me

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.