How to wait until BeginRefresh(...) has completed?

How to wait until BeginRefresh(...) has completed?

Old forum URL:

Gort posted on Thursday, December 16, 2010

Does anyone have any suggestions of the best way to wait for a BeginRefresh(...) call to complete before doing further operations?  Basically, I just want the async call to be synchronous for this particular case.  I've tried a couple options but am not getting things to work quite right.


RockfordLhotka replied on Thursday, December 16, 2010

What you are looking for is technically called a "continuation" - where you start the next task in a sequence when the previous task completes.

I assume you are doing this in a viewmodel object, and the easiest way to do this is to override OnRefreshed - that's the method which is invoked when BeginRefresh completes (or OnError if it fails - you really need to override both).

Gort replied on Friday, December 17, 2010

I am trying to instantiate a view model from my App.xaml.cs file and want the execution to wait until the view model instantiation is complete.

Here is my code in App.xaml.cs:

SiteViewModel svm = new SiteViewModel();

var x = svm.SomeProperty;

In SiteViewModel:

public SiteViewModel()

private void GetData()
    BeginRefresh("GetBySiteIdAsync", new object[] { 1 });

protected override void OnRefreshed()

OnRefreshed() does get called, but not until after execution has continued in the statement above so 'svm.SomeProperty' is always null/empty.

RockfordLhotka replied on Friday, December 17, 2010

Well you can't "wait" because you are living in an async world. To "wait" would mean blocking the UI thread, which is the whole reason Microsoft forces server access to be async - to prevent you from blocking the UI thread. While you could work around this and block the UI thread, that'd be a horrible thing to do since it would entirely lock up the browser.

Instead, you need to embrace the reality of working in an async world.

I typically don't try to load anything in app.xaml.cs - that's too early (and too hard to debug). Instead, I do my first loading of things in my main form or the first content control hosted in the main form.

And I always have some strategy for "locking out" user interaction on the main form when necessary. This often involves having a Canvas "overlay" that sits over the top of any content in my UI. I'll typically data bind this to some IsBusy flag on the viewmodel or presenter or whatever.

Copyright (c) Marimer LLC