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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:46:42+00:00 2026-05-17T20:46:42+00:00

I searched for a sample for this, but could not find something that explains

  • 0

I searched for a sample for this, but could not find something that explains clearly how to set it up using RX:
I have this requirement…

  1. In a WPF app, i have a list box
  2. A dispatcher timer routine adds some random numbers to a local List every say 2 seconds
  3. Now i want to setup an observable/observer to watch this List<int> as it keeps building up, and add the most recent number added, into the list box’s items collection.

Sounds very simple, and i have done the third bit in a background thread (no RX, but with a standard lookup on list<int>)and added to the listbox easily.
When trying to do the same without a background worker etc and just using RX, i am stuck.

Apologies for a possible stupid question (for you RX experts out there), but please help on how to get this WPF done using RX.

Thanks.

  • 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-17T20:46:42+00:00Added an answer on May 17, 2026 at 8:46 pm

    When working with Rx you need to keep in mind the duality between IEnumerable<T> & IObservable<T> (as well as IEnumerator<T> & IObserver<T>).

    You should always look for objects that implement IEnumerable<T> and consider how you would replace them with IObservable<T>.

    In your question you say that you have a timer adding some numbers to a List<int> which you want to observe and add the new numbers to a list box. So I would consider replacing the list with an IObservable<int>. The trick here isn’t about watching a list (or an ObservableCollection<int>) but instead it is about using Rx as a core part of your code.

    So, here’s a simple example.

    Start with the core elements described in your question:

    var dispatchTimer = new DispatcherTimer();
    var random = new Random();
    var listBox = new ListBox();
    

    Create an observable from dispatchTimer:

    IObservable<IEvent<EventArgs>> ticks =
        Observable.FromEvent(
            h => dispatchTimer.Tick += h,
            h => dispatchTimer.Tick -= h);
    

    Query the observable to create a new observable of random numbers:

    IObservable<int> randomNumbers =
        from tick in ticks
        select random.Next(1, 11);
    

    Now, subscribe to the random numbers observable to update the list box:

    _updateListBoxSubscription =
        randomNumbers.ObserveOnDispatcher().Subscribe(n => listBox.Items.Add(n));
    

    The .ObserveOnDispatcher() call will make sure that the numbers are added to the list box on the UI thread.

    You need to define a field or a property to hold a reference to the subscription so that it doesn’t get garbage collected. This is exactly what event handler fields do when you add a handler, but with Rx you must explicitly do it.

    private IDisposable _updateListBoxSubscription;
    

    There you go – you now have a list box being updated from random numbers generated at the interval specified in the time.

    It’s that simple. I hope this helps.

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

Sidebar

Related Questions

No related questions found

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.