Skip to content

Instantly share code, notes, and snippets.

@LanceMcCarthy
Last active July 21, 2016 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LanceMcCarthy/ff24994a00d536e64306d38325e47c22 to your computer and use it in GitHub Desktop.
Save LanceMcCarthy/ff24994a00d536e64306d38325e47c22 to your computer and use it in GitHub Desktop.
#region Background Task management
private async Task<bool> ConfigureBackgroundTaskAsync()
{
try
{
vm.IsBusy = true;
vm.IsBusyMessage = "configuring Background Task";
var accessStatus = await BackgroundExecutionManager.RequestAccessAsync();
switch (accessStatus)
{
case BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity:
case BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity:
await TaskHelpers.RegisterTaskAsync("FriendlyName", typeof(TaskClass).FullName, (uint) updateFrequency);
Debug.WriteLine($"Task is running every {updateFrequency} minutes");
return true;
case BackgroundAccessStatus.Unspecified:
Debug.WriteLine($"Task is currently NOT running");
await new MessageDialog(content: "You did not make a choice, please try again.").ShowAsync();
break;
case BackgroundAccessStatus.Denied:
Debug.WriteLine($"Task was DENIED access");
await
new MessageDialog("You've denied the app from updating in the background or you have too many background tasks already. " +
"r\n\nGo to Phone Settings > Background Apps \r\n\nFind this app in the list and re-enable background tasks.").ShowAsync();
break;
}
return false;
}
catch (Exception ex)
{
await new MessageDialog($"Something went wrong configuring the background task. Error: {ex.Message}").ShowAsync();
return false;
}
finally
{
vm.IsBusy = false;
vm.IsBusyMessage = "";
}
}
private async Task DisableTaskAsync()
{
try
{
vm.IsBusy = true;
vm.IsBusyMessage = "unregistering background task...";
//unregister the task and confirm to user it was successful
if (await TaskHelpers.UnregisterTaskAsync("FriendlyName"))
{
Debug.WriteLine($"Monitoring Task is currently NOT running");
}
}
catch (Exception ex)
{
await new MessageDialog($"There was a problem disabling Monitoring Task. Error: {ex.Message}").ShowAsync();
}
finally
{
vm.IsBusyMessage = "";
vm.IsBusy = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment