views:

85

answers:

1

I have the following code in a WPF application using Reactive Extensions for .NET:

public MainWindow()
{
    InitializeComponent();

    var leftButtonDown = Observable.FromEvent<MouseButtonEventArgs>(this, "MouseLeftButtonDown");
    var leftButtonUp = Observable.FromEvent<MouseButtonEventArgs>(this, "MouseLeftButtonUp");

    var moveEvents = Observable.FromEvent<MouseEventArgs>(this, "MouseMove")
        .SkipUntil(leftButtonDown)
        .SkipUntil(leftButtonUp)
        .Repeat()
        .Select(t => t.EventArgs.GetPosition(this));

    moveEvents.Subscribe(point =>
    {
        textBox1.Text = string.Format(string.Format("X: {0}, Y: {1}", point.X, point.Y));
    });
}

Will there be a steady increase of memory while the mouse is moving on this dialog?

Reading the code, I would expect that the moveEvents observable will contain a huge amount of MouseEventArgs after a while? Or is this handled in some smart way I'm unaware of?

+2  A: 

No, there shouldn't be a steady increase in memory usage - why would there be? The events are basically streamed to the subscriber; they're not being buffered up anywhere.

The point of Rx is that events are pushed to the subscriber, who can choose what to do with them. It's not like adding events to a list which is then examined later.

(There are ways of buffering events in Rx, but you're not using them as far as I can tell.)

Jon Skeet
Thanks for your answer. To somewhat answer your question "why would there be?" - I asked because I haven't yet understood how this works. It's not clear to me what happens in the line Observable.FromEvent<MouseButtonEventArgs>(this, "MouseLeftButtonDown");
OneOfAccount