---
title: "Reactive Extensions (Rx) - Part 7 - Sample Events - Muhammad Rehan Saeed"
description: "Shows how to use the Reactive Extensions (Rx) Sample method to reduce the number of subscribed events by taking a sample from them over a given time period."
canonical_url: "https://rehansaeed.com/reactive-extensions-part7-sample-events/"
last_updated: "2014-12-24T00:00:00+00:00"
meta:
  author: "Muhammad Rehan Saeed"
  keywords: ".NET,C#,Reactive Extensions,Rx"
  "og:description": "Shows how to use the Reactive Extensions (Rx) Sample method to reduce the number of subscribed events by taking a sample from them over a given time period."
  "og:image": "https://rehansaeed.com/_og/s/o_358d725c94ae420f.png"
  "og:image:alt": "Reactive Extensions (Rx) - Part 7 - Sample Events - Muhammad Rehan Saeed"
  "og:image:height": "630"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "Reactive Extensions (Rx) - Part 7 - Sample Events"
  "twitter:image": "https://rehansaeed.com/_og/s/o_358d725c94ae420f.png"
  "twitter:image:alt": "Reactive Extensions (Rx) - Part 7 - Sample Events - Muhammad Rehan Saeed"
---

> Index of every page on this site: [llms.txt](https://rehansaeed.com/llms.txt)

# [Reactive Extensions (Rx) - Part 7 - Sample Events](https://rehansaeed.com/reactive-extensions-part7-sample-events/)

Posted 24 December 2014 - **2 min read**

![Reactive Extensions](https://rehansaeed.com/_ipx/f_png&s_2560x1440/images/hero/Reactive-Extensions-1366x768.png)

1. [Reactive Extensions (Rx) - Replacing C# Events](https://rehansaeed.com/reactive-extensions-part1-replacing-events/)
2. [Reactive Extensions (Rx) - Wrapping C# Events](https://rehansaeed.com/reactive-extensions-part2-wrapping-events/)
3. [Reactive Extensions (Rx) - Naming Conventions](https://rehansaeed.com/reactive-extensions-part3-naming-conventions/)
4. [Reactive Extensions (Rx) - Replacing Timers](https://rehansaeed.com/reactive-extensions-part4-replacing-timers/)
5. [Reactive Extensions (Rx) - Awaiting Observables](https://rehansaeed.com/reactive-extensions-part5-awaiting-observables/)
6. [Reactive Extensions (Rx) - Task ToObservable](https://rehansaeed.com/reactive-extensions-part6-task-toobservable/)
7. [Reactive Extensions (Rx) - Sample Events](https://rehansaeed.com/reactive-extensions-part7-sample-events/)
8. [Reactive Extensions (Rx) - Timeouts](https://rehansaeed.com/reactive-extensions-rx-part-8-timeouts/)

Its been a while since I've done another Rx post. They've been pretty popular and thanks to the community for all the positive feedback. I was talking to a colleague yesterday who had been using standard C# events in WPF (The principals learned in this post can apply anywhere). He had subscribed to the `TextChanged` event in C# and was updating the user interface on the fly, whenever the user typed in a character of text. He was getting way too many events being fired and his user interface couldn't keep up with all the work it was being asked to do.

```cs
this.TextBox.TextChanged += this.OnTextBoxTextChanged;

private void OnTextBoxTextChanged(object sender, TextChangedEventArgs e)
{
    // Heavy User Interface updates that can cause the application to lock up.
}
```

This is a very common scenario which I myself have come across several times. The solution to this problem is to take a sample of the events being fired and only update the user interface every few seconds. This is possible without Reactive Extensions (Rx) but you have to write a fair amount of boilerplate code (I know, I've done it myself).

Reactive Extensions (Rx) can do this with a few easy to understand (This is the real bonus) lines of code. The first step is to wrap the WPF `TextChanged` event (I've shown how to do this in a previous post [here](https://rehansaeed.com/reactive-extensions-part2-wrapping-events/)).

```cs
public IObservable<TextChangedEventArgs> WhenTextChanged
{
    get
    {
        return Observable
            .FromEventPattern<TextChangedEventHandler, TextChangedEventArgs>(
                h => this.TextBox.TextChanged += h,
                h => this.TextBox.TextChanged -= h)
            .Select(x => x.EventArgs);
    }
}

this.WhenTextChanged
    .Sample(TimeSpan.FromSeconds(3))
    .Subscribe(x => Debug.WriteLine(DateTime.Now + " Text Changed"));
```

The final and most succinct step is to use the Sample method to only pick out the latest text changed event every three seconds and pass that on to the `Subscribe` delegate. It really is that easy and this blog post really is this short because of that!

## Web Mentions

[What's this?](https://en.wikipedia.org/wiki/Webmention)

### 0 Linked

### 0 Replies

## GitHub Comments

### 4 Comments

[Comment on GitHub](https://github.com/RehanSaeed/rehansaeed.github.io/issues/109)

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed)

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Roland Pheasant](https://camo.githubusercontent.com/5f3ffd8eac83369e2394563dc0f629e5951712124221ab7b4ed0cbfa2498865d/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f37653139353437633532613837363162656233346265343535613962636664633f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/5f3ffd8eac83369e2394563dc0f629e5951712124221ab7b4ed0cbfa2498865d/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f37653139353437633532613837363162656233346265343535613962636664633f733d393626643d6d6d26723d67) **[Roland Pheasant](http://dynamicdataproject.wordpress.com/)** commented on **2015-01-06 13:48:34**

This is promising to be a great tutorial series on RX. I think in many ways a lot of the rx guru's overly complicate rx so much so that many people are put off and bewildered. In future I will point people here.

Do you know about this http://dynamicdataproject.wordpress.com/

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed)

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Muhammad Rehan Saeed](https://camo.githubusercontent.com/8a07049df41b8a3839a3f4342f09ea8e6e7fa0065667687f04beca2d088a1dca/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32623266393030653361633066353535316462646537323864336262343935343f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/8a07049df41b8a3839a3f4342f09ea8e6e7fa0065667687f04beca2d088a1dca/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32623266393030653361633066353535316462646537323864336262343935343f733d393626643d6d6d26723d67) **[Muhammad Rehan Saeed](https://rehansaeed.com/)** commented on **2015-01-06 14:02:19**

> This is promising to be a great tutorial series on RX. I think in many ways a lot of the rx guru's overly complicate rx so much so that many people are put off and bewildered. In future I will point people here.
>
> Do you know about this http://dynamicdataproject.wordpress.com/

I had a lot of trouble learning Rx myself. These articles are my way of trying things out and seeing what works best. I'm still learning BTW.  
 The problem is, Rx is really that complicated! It can do so much but there is a massive amount of overlap with other API's. So although it can do so much, it doesn't mean you should use it.  
 I think when you cut out the bits you shouldn't use it for, it's a really great API.

I've not come across that project, I'll give it a read when I get a chance.

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed)

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Roland Pheasant](https://camo.githubusercontent.com/5f3ffd8eac83369e2394563dc0f629e5951712124221ab7b4ed0cbfa2498865d/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f37653139353437633532613837363162656233346265343535613962636664633f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/5f3ffd8eac83369e2394563dc0f629e5951712124221ab7b4ed0cbfa2498865d/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f37653139353437633532613837363162656233346265343535613962636664633f733d393626643d6d6d26723d67) **[Roland Pheasant](http://dynamicdataproject.wordpress.com/)** commented on **2015-01-06 15:30:50**

> I had a lot of trouble learning Rx myself. These articles are my way of trying things out and seeing what works best. I'm still learning BTW.  
>  The problem is, Rx is really that complicated! It can do so much but there is a massive amount of overlap with other API's. So although it can do so much, it doesn't mean you should use it.  
>  I think when you cut out the bits you shouldn't use it for, it's a really great API.
>
> I've not come across that project, I'll give it a read when I get a chance.

I agree there is a steep learning curve but once the penny drops then it is full steam ahead. I personally find coding without it now to be alien.

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed)

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Muhammad Rehan Saeed](https://camo.githubusercontent.com/8a07049df41b8a3839a3f4342f09ea8e6e7fa0065667687f04beca2d088a1dca/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32623266393030653361633066353535316462646537323864336262343935343f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/8a07049df41b8a3839a3f4342f09ea8e6e7fa0065667687f04beca2d088a1dca/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32623266393030653361633066353535316462646537323864336262343935343f733d393626643d6d6d26723d67) **[Muhammad Rehan Saeed](https://rehansaeed.com/)** commented on **2015-01-06 15:41:01**

> I agree there is a steep learning curve but once the penny drops then it is full steam ahead. I personally find coding without it now to be alien.

Totally agree. Its just like Linq. Losing it is like losing an arm.

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://rehansaeed.com/#website","@type":"WebSite","description":"Software Developer at Microsoft, YouTuber, Open Source Contributor and Blogger","inLanguage":"en-GB","name":"Muhammad Rehan Saeed","url":"https://rehansaeed.com/","author":{"@id":"https://rehansaeed.com/#identity"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://rehansaeed.com/?search={search_term_string}"},"query-input":"required name=search_term_string"}],"publisher":{"@id":"https://rehansaeed.com/#publisher"}},{"@id":"https://rehansaeed.com/reactive-extensions-part7-sample-events/#webpage","@type":"WebPage","dateModified":"2014-12-24T00:00:00+00:00","datePublished":"2014-12-24T00:00:00+00:00","description":"Shows how to use the Reactive Extensions (Rx) Sample method to reduce the number of subscribed events by taking a sample from them over a given time period.","name":"Reactive Extensions (Rx) - Part 7 - Sample Events","url":"https://rehansaeed.com/reactive-extensions-part7-sample-events/","breadcrumb":{"@id":"https://rehansaeed.com/reactive-extensions-part7-sample-events/#breadcrumb"},"isPartOf":{"@id":"https://rehansaeed.com/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://rehansaeed.com/reactive-extensions-part7-sample-events/"]}]},{"@id":"https://rehansaeed.com/#identity","@type":"Person","name":"Muhammad Rehan Saeed","url":"https://rehansaeed.com/about/","image":{"@id":"https://rehansaeed.com/#/schema/image/1"},"sameAs":["https://github.com/RehanSaeed","https://www.linkedin.com/in/muhammad-rehan-saeed","https://stackoverflow.com/users/1212017/muhammad-rehan-saeed","https://www.youtube.com/c/MuhammadRehanSaeed","https://www.twitch.tv/rehansaeeduk"]},{"@id":"https://rehansaeed.com/#publisher","@type":"Organization","logo":"https://rehansaeed.com/images/schema/Publisher-600x60.png","name":"Muhammad Rehan Saeed","url":"https://rehansaeed.com"},{"@id":"https://rehansaeed.com/reactive-extensions-part7-sample-events/#article","@type":"Article","dateModified":"2014-12-24T00:00:00+00:00","datePublished":"2014-12-24T00:00:00+00:00","description":"Shows how to use the Reactive Extensions (Rx) Sample method to reduce the number of subscribed events by taking a sample from them over a given time period.","headline":"Reactive Extensions (Rx) - Part 7 - Sample Events","inLanguage":"en-GB","url":"https://rehansaeed.com/reactive-extensions-part7-sample-events/","author":{"@id":"https://rehansaeed.com/#identity"},"image":[{"@type":"ImageObject","url":"https://rehansaeed.com/images/hero/Reactive-Extensions-1366x768.png","caption":"Reactive Extensions","width":"1366","height":"768"}],"isPartOf":{"@id":"https://rehansaeed.com/reactive-extensions-part7-sample-events/#webpage"},"keywords":[".NET","C#","Reactive Extensions","Rx"],"mainEntityOfPage":{"@id":"https://rehansaeed.com/reactive-extensions-part7-sample-events/#webpage"},"publisher":{"@id":"https://rehansaeed.com/#publisher"}},{"@id":"https://rehansaeed.com/reactive-extensions-part7-sample-events/#breadcrumb","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","name":"Blog","item":"https://rehansaeed.com/","position":1},{"@type":"ListItem","name":"Reactive Extensions (Rx) - Part 7 - Sample Events","item":"https://rehansaeed.com/reactive-extensions-part7-sample-events/","position":2}]},{"@id":"https://rehansaeed.com/#/schema/image/1","@type":"ImageObject","contentUrl":"https://rehansaeed.com/images/author/Muhammad-Rehan-Saeed/Logo-512x512.png","inLanguage":"en-GB","url":"https://rehansaeed.com/images/author/Muhammad-Rehan-Saeed/Logo-512x512.png"}]}
```

## Sitemap

See the full [sitemap](https://rehansaeed.com/sitemap.md) for all pages.
