Windows Support Number

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Monday, 17 June 2013

Implementing a busy indicator using a visual overlay in MVVM

Posted on 13:24 by Unknown
This is a technique we use at work to lock the UI whilst some long running process is happening - preventing the user clicking on stuff whilst it's retrieving or rendering data. Now we could have done this by launching a child dialog window but that feels rather out of date and clumsy, we wanted a more modern pattern similar to the way <div> overlays are done on the web.

Imagine we have the following simple WPF app and when 'Click' is pressed a busy waiting overlay is shown for the duration entered into the text box. What I'm interested in here is not the actual UI element of the busy indicator but how I go about getting this to show & hide from when using MVVM. The actual UI elements are the standard Busy Indicator coming from the WPF Toolkit:
The XAML behind this window is very simple, the important part is the ViewHost. As you can see the ViewHost uses a ContentPresenter element which is bound to the view model, IMainViewModel, it contains 3 child view models, one for the top, middle & bottom.
The ViewHost is nothing more than a ContentControl, its not only a place holder for the desired content but it also has the overlay and progress bar shown above.
Where is the XAML for the overlay defined?

The XAML is defined in a style - see below, I've defined it like this so I don't have to explicitly write any extra XAML when I want to use the ViewHost control - if I had an app with multiple Views I wouldn't have to keep defining the Grid for the overlay.

As you can see I have a specific view model - IBusyViewModel, this has a property called BusyMonitor which is used to trigger both the visibility of the grid containing the busy overlay as well as the actual overlay. I do this because the BusyMonitor property on the IBusyViewModel could be null and I don't want a binding exception thrown if I bound to BusyMonitor.IsBusy and BusyMonitor is null.
As you can see I have a specific view model - IBusyViewModel, this is bindable obejct because the IsBusy property is bound to the busy overlay. I've also included an Rx observable to allow any ViewModel to observe when the value changes.
Instances of this interface are then injected into the MainViewModel instance:
Also injected into the ContentViewModel & FooterViewModel, in this example these view models don't do very much:
The interesting view model in this example is the HeaderViewModel. The IObservableCommand handles the button click, which executes the ExecuteClick method. This sets the BusyMonitor.IsBusy property to true initially and when the observable timer pumps the BusyMonitor.IsBusy is set back to false.
Now at this point you might be thinking...

'but you've got 4 view models and 4 instances of the IBusyMonitor interface, how can setting IsBusy to true in the HeaderViewModel affect the whole application?'

The answer in fact is the exact opposite, they are all sharing the same instance of the IBusyMonitor interface and this is achieved by using an IoC container with a singleton registration for the IBusyMonitor interface, I'm using AutoFac for this example:
That pretty much rounds it up, the code is available for download:
Read More
Posted in .Net, Development, MVVM, WPF | No comments

Saturday, 15 June 2013

Comparing performance of .Net 4.5 to .Net 4.0 for WPF

Posted on 13:52 by Unknown
Currently I'm working on a .Net 4.0 WPF app and we've had some discussion about moving to .Net 4.5, we don't get to make the decision :( but we're interested to know what performance gain we'd get.

What follows is a comparison of the same app compiled against .Net 4.0 & 4.5. The app makes use of a couple of NuGet packages - Autofac & Reactive Extensions, both of which provide versions for both .Net 4.0 & 4.5. It also makes use of Telerik grid control to render the data.

What does the app do?

Pretty simple, it generates a 1000 rows of data asynchronously and binds this to a telerik grid, for every iteration the grid is clear of any data first.
How am I going to measure any performance improvement?

I'm using the code from a previous post - 'Measuring UI Freeze...', I'm expecting any improvement to be visible in a reduced amount of time on the dispatcher thread - if the (.Net) code base is more efficient it should surely reduce the amount of time required to render the UI.

I'm going to repeat the generation and rendering of data 100 times for each version of the framework, this should allow an statistical anomalies to be ignored.

How am I going to record any improvement?

Simply write out to file any value that exceeds 1000 milliseconds, this is done in the App_Startup method:
So what's the outcome?

The outcome is best visualised as a set of graphs, first a scatter graph where x-axis represents the iteration and y-axis represents the UI freeze duration in milliseconds:
and more impressively a bell curve,where the x-axis represents the duration in milliseconds of any UI freeze and frequency on the y-axis:
Averages are as follows:

.Net 4.0 average = 1605 ms
.Net 4.5 average = 1337 ms (elite :))

Or to put it another way this is a 16% increase in performance!

Okay so this wasn't anywhere near a scientific approach but it did show over 100 iterations there is measurable improvement, I've made the code available for the .Net 4.5 version.

Read More
Posted in .Net, Development, MVVM, Performance, WPF | No comments

Saturday, 1 June 2013

Measuring UI freeze in WPF applications with Reactive Extensions

Posted on 09:46 by Unknown
Making sure an application doesn't freeze the UI is probably one of the most important concerns when building an application. UI freeze can be broken down into two categories,firstly long running background tasks and secondly the rendering of UI elements (controls) to the screen.

The second situation is what interests me with this post, usually UI based applications will dedicate a single thread to rendering the UI (dispatcher). This thread in theory can become overloaded with work and therefore the UI  becomes unresponsive to the user until the work is completed - the app freezes. Measuring this freeze on the dispatcher is useful in diagnosing the problems of rendering large amounts of data in a short time frame, e.g. like trying to render a large number of rows in a grid.

How can I measure this freeze?

You can do this easily with a couple of Rx methods:
Loading ....
The key is making sure they execute on the dispatcher thread. This ensures that when the dispatcher is overloaded they will not fire at the expected interval and therefore the interval between current & previous will be large enough for the stream to pump. This is then used at startup:
Loading ....
The following simple UI simulates the affect of overloading the dispatcher thread, you can see I've routed the button click through to a method which does a thread sleep on the dispatcher thread:
Loading ....
When the button is clicked serveral times I get the following output:

Read More
Posted in Coding C#, Development, Rx, UI, WPF | No comments
Newer Posts Older Posts Home
Subscribe to: Posts (Atom)

Popular Posts

  • Unit testing Rx methods Timeout & Retry with moq
    Earlier this week I was trying to unit test an asynchronous service (Foo) which used another asynchronous service (Bar) internally and ran i...
  • Understanding RefCount in Reactive Extensions
    A couple of weeks ago  @LordHanson  & I ran into an issue converting a stateless async service exposed as an Rx cold observable to a  co...
  • StructureMap: ILifecycle
    The other day I wanted to control the scope of a service inside a web based app with semantics which didn't fit either 'HttpContextS...
  • MVVM anti-pattern: Injecting the IoC container into a View Model
    This is another anti-pattern I've seen a lot recently, the dynamic use of the IoC container inside a view model to resolve child view mo...
  • How many pins can Bing Maps handle in a WP7 app - part 1
    part2 -  http://awkwardcoder.blogspot.com/2011/10/how-many-pins-can-bing-maps-handle-in.html part3 -  http://awkwardcoder.blogspot.com/2011/...
  • Bad developers love 'The Daily WTF'
    When 'The Daily WTF' started up back in 2003/2004 it was a great laugh looking at shocking code other developers wrote, but after a ...
  • Using CompositeDisposable in base classes
    To help make an object eligible for collection by the GC (garbage collector) one would implement the IDisposable interface. Executing the di...
  • Implementing a busy indicator using a visual overlay in MVVM
    This is a technique we use at work to lock the UI whilst some long running process is happening - preventing the user clicking on stuff whil...
  • Daily Dilbert Service - the most important service I've ever written...
    NuGet package available here ... First off a big shout to  @hamish  &  @leeoades  on this one - I'm just blogging about it. At work ...
  • Comparing performance of .Net 4.5 to .Net 4.0 for WPF
    Currently I'm working on a .Net 4.0 WPF app and we've had some discussion about moving to .Net 4.5, we don't get to make the dec...

Categories

  • .Net
  • .Net 4.5
  • Abstractions
  • Advertising
  • Agile
  • Agile Courage
  • AOP
  • Async
  • automated testing
  • Azure
  • Azure IIS RESTful development
  • BDD
  • Bing Maps
  • Bounded Context
  • C#
  • C# 5.0
  • Caching
  • Chocolatey
  • CLoud
  • CodePlex
  • Coding
  • Coding Building CI Testing
  • Coding C#
  • coding C# IoC StructureMap
  • Coding Functional-Programming
  • Coding REST Knowledge
  • Coding Services
  • Coding TDD Refactoring Agile
  • Command
  • continuous testing
  • coupling
  • CultureInfo
  • DAL
  • databases
  • DDD
  • DDD Coaching
  • DDD Domain Events Auditing nHibernate
  • DDD Entities Value Objects
  • Debugging
  • Design Patterns
  • Design Patterns Databases Auditing
  • Developement
  • Development
  • Development Coding
  • Development Process
  • Development unit testing
  • Development VS 2011
  • Diagnostics
  • Disposable
  • Exceptions
  • FINDaPAD
  • FindaPad Property Rental Windows Phone 7 Mobile Devices
  • Fun Coding Duct-Tape
  • Hotfixes
  • integration testing
  • IoC
  • jasmine
  • javascript
  • Jobs Development
  • LINQ
  • marketplace
  • Mobile Devices
  • Mocking
  • MSDN Coding
  • MSpec
  • Multilingual
  • MVC
  • MVVM
  • nCrunch
  • nHbiernate Repository Pattern Criteria
  • nHibernate Auditing Design Fluent
  • nHibnerate Entities Events Listeners
  • node.js
  • nodes.js
  • Nokia
  • NoSQL RavenDB Azure Development
  • Observations
  • OO
  • ORM
  • Performance
  • Portable Class Library
  • Portable Library
  • PostSharp
  • Process
  • Rants
  • RavenDB IIS 7.5 Development
  • Reactive
  • Reactive Extension
  • Reactive Extensions
  • ReadOnlyCollections
  • Resharper
  • REST Distributed-Systems
  • REST HTTP
  • rest web
  • RESTful
  • Rx
  • Serialization
  • Silverlight
  • Silverlight Installation
  • Task
  • TDD
  • TDD IoC DI
  • TDD Mocking
  • TDD Team Observation
  • Telerik
  • testing
  • threading
  • TPL
  • UI
  • Undo-Redo
  • unit testing
  • ViewModels
  • VS 2012
  • wcf
  • web api
  • Web Services
  • web services mobile devices data
  • WebAPI
  • Windows
  • Windows 8
  • windows phone
  • Windows Phone 7
  • WP7
  • WP7 Bing Maps Development Network HTTP
  • WP7 Bing Maps Development UK Crime
  • WP7 Bing Maps Development UK Crime Clustering
  • WP7 Bing Maps Development UK Polygons Clustering Performance
  • WP7 cryptography bouncy castle
  • WP7 Cultures C#
  • WP7 feedback development app store
  • WP7 Javascript web browser
  • WP7 MSBuild
  • WP7 ORM Databases performance
  • WP7 Serialisation
  • WP7 SilverlightSerializer C#
  • WP7 sqlite performance development
  • WP7 WP7Contrib Bing Maps Development
  • WP7 WP7Contrib Bing Maps Polygon Development
  • WP7 WP7Contrib CodePlex
  • WP7 WP7Contrib CodePlex Bing Maps Development
  • WP7 WP7Contrib CodePlex ObservableCollection
  • WP7 WP7Contrib ILMerge .Net
  • WP7 WP7Contrib Phone Maps
  • WP7 WP7Contrib SilverlightSerializer C#
  • WP7Contrib
  • WP7Contrib Bing Maps WP7
  • WP7Contrib WP7 Geo-Location development C#
  • WP7Contrib WP7 HTTP Compression
  • WP7Contrib WP7 Url Development Rx
  • WP7Dev
  • WPF
  • WPF Cultures
  • WuApi
  • XAML

Blog Archive

  • ▼  2013 (16)
    • ►  November (5)
    • ►  September (3)
    • ►  August (1)
    • ►  July (1)
    • ▼  June (3)
      • Implementing a busy indicator using a visual overl...
      • Comparing performance of .Net 4.5 to .Net 4.0 for WPF
      • Measuring UI freeze in WPF applications with React...
    • ►  May (2)
    • ►  January (1)
  • ►  2012 (44)
    • ►  November (2)
    • ►  October (8)
    • ►  September (5)
    • ►  August (2)
    • ►  July (4)
    • ►  June (3)
    • ►  May (1)
    • ►  April (2)
    • ►  March (13)
    • ►  February (4)
  • ►  2011 (52)
    • ►  December (3)
    • ►  November (5)
    • ►  October (7)
    • ►  September (7)
    • ►  August (11)
    • ►  July (4)
    • ►  May (2)
    • ►  April (1)
    • ►  March (5)
    • ►  February (3)
    • ►  January (4)
  • ►  2010 (1)
    • ►  August (1)
  • ►  2009 (32)
    • ►  December (3)
    • ►  November (7)
    • ►  October (6)
    • ►  September (11)
    • ►  April (1)
    • ►  March (4)
Powered by Blogger.

About Me

Unknown
View my complete profile