Focus On Delivering Value to Your Patients.

Dispatching in Silverlight

Contact Us Today

Anyone who has been building Silverlight and/or WPF applications for some time understands that there is a single UI thread that has special access requirements if you are going to be updating elements. In other words, if I spin off a new thread, I cannot arbitrarily impact the UI unless I get back to the UI thread using a dispatcher.

Fortunately, Silverlight makes it very easy to find your dispatcher, because it is a property on any UIElement that exists, including your own user control. If I need to make sure a thread executes on the UI thread, I simply do something like this:

this.Dispatcher.BeginInvoke(()=>MyFunc()); 

Things get a little more tricky if you are trying to access the dispatcher from an entity that doesn’t know about the UI thread … for example, a view model.

Let’s say you have a view model that needs to kick of a separate thread to do some work, and then update properties that are bound to the UI. By design, the view model interacts with services and exposes properties but it doesn’t know about the view. We could sell out by giving it a Dispatcher property, but that could get ugly in scenarios such as unit tests where there simply may not be a UI thread.

Once again I’m happy to go back to the C# keywords that make our lives easier. In the past, I would have simply raised an event, “I need this” and passed some arguments, but with Action at our fingertips, it’s so much easier!

In fact, this is all it really takes … note the use of an action that takes a, um, ahem, action:

public class ViewModel 
{

   public Action<Action> UIAction { get; set; }

   public ViewModel()
   {
      UIAction = ((uiAction)=>uiAction());
   }

}

Seems fairly simple, doesn’t it? Any time I am going to update one of my databound properties in a disconncted thread, I simply do this:

UIAction(()=>{ viewModel.Property = "New value";}); 

Right now, this isn’t doing anything really interesting, because the action delegate simply calls the action we pass … very boring, really. It will pass unit tests and all of that jazz … so why’d I do it?

Because, once I bring the view model into an actual view, then I’ll need to give it the dispatcher. Then, I can do something like this:

ViewModel viewModel = new ViewModel();
this.DataContext = viewModel; // or bind it with MEF to make it more fun
viewModel.UIAction = ((uiAction) => Dispatcher.BeginInvoke(()=>uiAction())); 

Now we’ve set it up so that when you call the action with an action, the action is to perform the action inside the dispatcher thread (got all that?). Now the view model plays equally well in the view and out of the view. It is still ignorant of what the view is or even that there is a dispatcher, and only needs to know that updating protected properties should be wrapped in the UIAction.

Let’s save the argument over whether we should even be spinning threads from the view model that modify the UI thread for another day, mmmmm kay?

Jeremy Likness

Take advantage of our free assessment and Azure credits.

We understand that evolving your IT can be costly.
We are offering you a unique opportunity to save you money and get you started faster.

Get $2,500 Azure Credits and a Free Architecture Roadmap

Assess and Migrate Your Existing Environment

Improve Your Information Security and Compliance

Get up to 5 hours of architecture work and $2,500 worth of Azure credits, free of charge.

We right-size your systems and move them into an optimized environment.

We work closely with your team to develop the right roadmap.

Atmosera cited by Gartner as a Representative Vendor in the Market Guide for Cloud Service Providers to Healthcare Delivery Organizations

We help healthcare solution providers make the most of the cloud.

We are a Gold certified Microsoft Azure service provider offering a comprehensive set of managed services.
We excel at working with care providers and their ecosystem of solution providers.

Collaborate with Experts

Migrate Environments

Maintain Confidence

We take on your infrastructure concerns as an extension of your team and tackle compliance including HIPAA/HITECH and HITRUST.

Our methodology encompasses design through deployment and focuses on delivering solutions which are realistically implementable.

Rely on our deep knowledge of critical security frameworks and 24x7x365 monitoring by a team of experts.

We Know Healthcare

We are fortunate to count a number of leading care providers and healthcare vendors as our customers. We continue to grow this segment as companies turn to secure and compliant cloud solutions.

Our Customers Include:

All Care Services, CarePayment Technologies, Consonus Healthcare, Digital Visions, Fanno Creek Clinic, First Insight, Health Share of Oregon, Marquis Companies, MediPro Direct, Metropolitan Pediatrics, Navos, OCHIN, Oregon State Hospital, Planned Parenthood, ProtoCall Services, Salem Health, Seasons Management, The Portland Clinic, Touchmark, Vistalogic, WVP Health Authority.

View Data Sheet View Case Study

Collaborate with Experts

  • Plan out your cloud strategy without having to commit all your applications to the public cloud
  • Microsoft has the only viable hybrid strategy and expected to surpass AWS in market share by 2019.
  • We specialize in engineering, deploying and operating the right solution for your business by combining public and private Azure.
  • As one of the world’s five largest Microsoft cloud solution providers (CSP), we help you identify the optimal environment to run each application including your database and storage.
  • Meet HIPAA/HITECH, HITRUST and PCI DSS compliance

Migrate Environments

  • We have expertise which minimizes redevelopment to move applications and data without recoding.
  • We leverage Microsoft Azure Site Recovery (ASR) which provides a simple way to move applications and data without having to redevelop the underlying cloud infrastructure.
  • We implement your new environment on a public or private cloud depending on your preferences and business requirements.
  • We enable access to complete and accurate PHI for better medical care.

Maintain Confidence

  • Ensure you are secure from design through deployment.
  • Eliminate concerns about exposing your PHI and Non-PHI data when using the public cloud.
  • Define your objectives and build the right foundation using best practices and then execute it.
  • You gain the peace of mind that comes from knowing we have planned out your cloud to the smallest details and then made sure it delivers on your needs.
  • Let our team monitor your environment and take real-time actions to keep your applications running at their best.

We deliver solutions that accelerate the value of Azure.

Ready to experience the full power of Microsoft Azure?

Start Today

Blog Home

Stay Connected

Upcoming Events

All Events