Skip to content

Instantly share code, notes, and snippets.

Created February 21, 2014 03:39
Show Gist options
  • Save anonymous/9128333 to your computer and use it in GitHub Desktop.
Save anonymous/9128333 to your computer and use it in GitHub Desktop.
Shitty background worker and incompetent async practices
public class MainPage : MyAppPageBase
{
public Budget()
{
InitializeComponent();
}
public async override Task MyAppPageDoWork()
{
if (_isNewLoad)
{
_viewModel = await DeserializePreviousRunsViewModel();
DataContext = _viewModel;
}
}
public async override Task MyAppPageDoWorkCompleted()
{
// Initialize the view model
if (_isNewLoad)
{
_viewModel = await new MainPageViewModel();
DataContext = _viewModel;
_isNewLoad = false;
}
// other page loaded specific stuff
}
}
public class MyAppPageBase : PhoneApplicationPage, IMyAppPage
{
void MyAppPageBase_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
var bg = new BackgroundWorker();
bg.DoWork += delegate
{
Dispatcher.BeginInvoke(async delegate
{
await MyAppPageDoWork();
});
};
bg.RunWorkerCompleted += delegate
{
Dispatcher.BeginInvoke(async delegate
{
await MyAppPageDoWorkCompleted();
if (ApplicationBar != null)
ApplicationBar.IsVisible = true;
});
};
bg.RunWorkerAsync();
}
public abstract Task MyAppPageDoWork();
public abstract Task MyAppPageDoWorkCompleted();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment