Showing posts with label Reactive Extensions (RX). Show all posts
Showing posts with label Reactive Extensions (RX). Show all posts

Thursday, February 26, 2015

Using ImportMany For Lazy Initialisation With Reactive Extensions (RX) and WPF/Prism




Working on a WPF application, I had a requirement to allow a user to monitor the status of a number of connected services – the status of some services could fluctuate many times per second.


There were a couple of issues that needed to be considered:

  1. How would I get hold of these monitored ‘services’?  Could I just create new instances and pass them into my monitoring class?  Unfortunately not, as I didn’t know until runtime what these services were.
     
  2. How would I throttle the status updates to make them more presentable to the user without resulting in some sort of flashing Christmas tree effect?
      
  3. How would I unit test the monitoring behaviour?
     
  4. How would I display the status of specific items in an easy to understand manner?  Did I want to wait for a disconnect status and show that in, say, red? 

Wednesday, January 21, 2015

WPF Frame Rate Calculator

I've been playing around with WPF's CompositionTarget.Rendering event.
From the documentation:

CompositionTarget.Rendering Occurs just before the objects in the composition tree are rendered.The Rendering event is routed to the specified event handler after animation and layout have been applied to the composition tree.
From this event, I've been looked at a way to calculate how many frames are being rendered per second in a WPF application.

Caveat Emptor:
  1. CompositionTarget.Rendering is a static member, so remember to unhook your delegate when you don't need it...leaks....
  2. The act of adding a delegate to CompositionTarget.Rendering might slow your WPF application down...so it's probably best used debugging performance, or as a user configurable options when used in a production environment.  
  3. My calculations aren't very scientific..but the numbers produced are close to those seen by attaching to the application with Perforator from WPF Performance Suite.
This could be handy if a low frame rate is detected, you could log the time stamp (using something like log4net) and compare what was going on in the rest of you app.

Wednesday, January 14, 2015

Using RefCount To Create Automatic Disposal and Lazy Connections

This is quite a common scenario, I need to create a source data provider from some type of 'expensive' service, eg a stock market price ticker, which will automatically handle the job of connecting and disconnecting as the number of active observers changes.

By 'expensive' we could mean resource-wise expensive, ie amount of memory/ cpu time or financially expensive...perhaps there's a charge levied per subscription.

As an example, let's say you have a trading application with the following requirements:
  • User can open any number of trade entry screens.  
  • The trade entry screen allows the user to enter a stock symbol, eg MSFT
  • Once a symbol has been entered the application should subscribe to a market data source, eg Reuters, for that symbol and display the current price
  • If multiple screens request the same symbol then only one underlying subscription should be utilised - don't go creating yet another subscription for the same symbol
  • When a screen is closed or another symbol is selected, then we should notified when there no longer any active subscribers, thus allowing us to release the underlying source subscription
  • Subsequent requests to an already subscribed symbol should immediately send back the latest price received rather than wait until a new price arrives.