Created
February 21, 2014 03:39
-
-
Save anonymous/9128333 to your computer and use it in GitHub Desktop.
Shitty background worker and incompetent async practices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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