UWP Background Task - Application Trigger - Cannot create a file when that file already exists
In UWP I'm trying to create a simple background task on demand. So when I click a button, I want a background task to kick off and process some files in the background.
Below is what my basic Task code is:
public class BackgroundSyncService
{
private ApplicationTrigger trigger = new ApplicationTrigger();
public async Task Start()
{
if (IsRegistered())
Deregister();
BackgroundExecutionManager.RemoveAccess();
// does this prompt everytime?
await BackgroundExecutionManager.RequestAccessAsync();
var builder = new BackgroundTaskBuilder();
builder.Name = "BackgroundTask";
builder.TaskEntryPoint = typeof(BackgroundTask).FullName;
builder.SetTrigger(trigger);
BackgroundTaskRegistration task = builder.Register();
task.Completed += Task_Completed;
var result = await trigger.RequestAsync();
}
private void Task_Completed(BackgroundTaskRegistration sender, BackgroundTaskCompletedEventArgs args)
{
// alert UI
}
public void Stop()
{
Deregister();
}
private void Deregister()
{
var taskName = "BackgroundTask";
foreach (var task in BackgroundTaskRegistration.AllTasks)
if (task.Value.Name == taskName)
task.Value.Unregister(true);
}
private bool IsRegistered()
{
var taskName = "BackgroundTask";
foreach (var task in BackgroundTaskRegistration.AllTasks)
if (task.Value.Name == taskName)
return true;
return false;
}
}
When the code hits builder.Register()
it throws the following exception:
System.Exception: 'Cannot create a file when that file already exists.
(Exception from HRESULT: 0x800700B7)'
Not sure what I'm doing wrong here or what this error means in context of a Background Task?
uwp background-process background-task
add a comment |
In UWP I'm trying to create a simple background task on demand. So when I click a button, I want a background task to kick off and process some files in the background.
Below is what my basic Task code is:
public class BackgroundSyncService
{
private ApplicationTrigger trigger = new ApplicationTrigger();
public async Task Start()
{
if (IsRegistered())
Deregister();
BackgroundExecutionManager.RemoveAccess();
// does this prompt everytime?
await BackgroundExecutionManager.RequestAccessAsync();
var builder = new BackgroundTaskBuilder();
builder.Name = "BackgroundTask";
builder.TaskEntryPoint = typeof(BackgroundTask).FullName;
builder.SetTrigger(trigger);
BackgroundTaskRegistration task = builder.Register();
task.Completed += Task_Completed;
var result = await trigger.RequestAsync();
}
private void Task_Completed(BackgroundTaskRegistration sender, BackgroundTaskCompletedEventArgs args)
{
// alert UI
}
public void Stop()
{
Deregister();
}
private void Deregister()
{
var taskName = "BackgroundTask";
foreach (var task in BackgroundTaskRegistration.AllTasks)
if (task.Value.Name == taskName)
task.Value.Unregister(true);
}
private bool IsRegistered()
{
var taskName = "BackgroundTask";
foreach (var task in BackgroundTaskRegistration.AllTasks)
if (task.Value.Name == taskName)
return true;
return false;
}
}
When the code hits builder.Register()
it throws the following exception:
System.Exception: 'Cannot create a file when that file already exists.
(Exception from HRESULT: 0x800700B7)'
Not sure what I'm doing wrong here or what this error means in context of a Background Task?
uwp background-process background-task
add a comment |
In UWP I'm trying to create a simple background task on demand. So when I click a button, I want a background task to kick off and process some files in the background.
Below is what my basic Task code is:
public class BackgroundSyncService
{
private ApplicationTrigger trigger = new ApplicationTrigger();
public async Task Start()
{
if (IsRegistered())
Deregister();
BackgroundExecutionManager.RemoveAccess();
// does this prompt everytime?
await BackgroundExecutionManager.RequestAccessAsync();
var builder = new BackgroundTaskBuilder();
builder.Name = "BackgroundTask";
builder.TaskEntryPoint = typeof(BackgroundTask).FullName;
builder.SetTrigger(trigger);
BackgroundTaskRegistration task = builder.Register();
task.Completed += Task_Completed;
var result = await trigger.RequestAsync();
}
private void Task_Completed(BackgroundTaskRegistration sender, BackgroundTaskCompletedEventArgs args)
{
// alert UI
}
public void Stop()
{
Deregister();
}
private void Deregister()
{
var taskName = "BackgroundTask";
foreach (var task in BackgroundTaskRegistration.AllTasks)
if (task.Value.Name == taskName)
task.Value.Unregister(true);
}
private bool IsRegistered()
{
var taskName = "BackgroundTask";
foreach (var task in BackgroundTaskRegistration.AllTasks)
if (task.Value.Name == taskName)
return true;
return false;
}
}
When the code hits builder.Register()
it throws the following exception:
System.Exception: 'Cannot create a file when that file already exists.
(Exception from HRESULT: 0x800700B7)'
Not sure what I'm doing wrong here or what this error means in context of a Background Task?
uwp background-process background-task
In UWP I'm trying to create a simple background task on demand. So when I click a button, I want a background task to kick off and process some files in the background.
Below is what my basic Task code is:
public class BackgroundSyncService
{
private ApplicationTrigger trigger = new ApplicationTrigger();
public async Task Start()
{
if (IsRegistered())
Deregister();
BackgroundExecutionManager.RemoveAccess();
// does this prompt everytime?
await BackgroundExecutionManager.RequestAccessAsync();
var builder = new BackgroundTaskBuilder();
builder.Name = "BackgroundTask";
builder.TaskEntryPoint = typeof(BackgroundTask).FullName;
builder.SetTrigger(trigger);
BackgroundTaskRegistration task = builder.Register();
task.Completed += Task_Completed;
var result = await trigger.RequestAsync();
}
private void Task_Completed(BackgroundTaskRegistration sender, BackgroundTaskCompletedEventArgs args)
{
// alert UI
}
public void Stop()
{
Deregister();
}
private void Deregister()
{
var taskName = "BackgroundTask";
foreach (var task in BackgroundTaskRegistration.AllTasks)
if (task.Value.Name == taskName)
task.Value.Unregister(true);
}
private bool IsRegistered()
{
var taskName = "BackgroundTask";
foreach (var task in BackgroundTaskRegistration.AllTasks)
if (task.Value.Name == taskName)
return true;
return false;
}
}
When the code hits builder.Register()
it throws the following exception:
System.Exception: 'Cannot create a file when that file already exists.
(Exception from HRESULT: 0x800700B7)'
Not sure what I'm doing wrong here or what this error means in context of a Background Task?
uwp background-process background-task
uwp background-process background-task
asked May 2 '18 at 2:37
aherrickaherrick
9,3162388142
9,3162388142
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
For your issue, it is because you have a file with the specified name already exists in the current folder when you create the file. You can try to specify the NameCollisionOption when you create the file.
var file = await ApplicationData.Current.LocalFolder.
CreateFileAsync("MyFile", CreationCollisionOption.ReplaceExisting);
As your code in your example project, you have used the ApplicationData.LocalSettings, when you add the setting key, you should check whether the key is existing.
var settings = Windows.Storage.ApplicationData.Current.LocalSettings;
if (!settings.Values.ContainsKey("BackgroundTask"))
{
settings.Values.Add("BackgroundTask", "Hello from UWP");
}
Or you can use the ApplicationData.LocalSettings as following to use the key and change its value,
var settings = Windows.Storage.ApplicationData.Current.LocalSettings;
settings.Values["BackgroundTask"] = "Hello from UWP";
You can also look into the Store and retrieve settings and other app data topic.
Thanks for the answer. However I'm seeing this behavior even after uninstall the UWP app completely and having VS redeploy. This is a Xamarin Forms app if that matters. It does skip over the "IsRegistered" call so it's definitely not registered on first load of a fresh app.
– aherrick
May 3 '18 at 17:49
I can not reproduce this issue in my side. I used the scenario in my answer, I will not get the error again. Could you provide a minimum sample to help me see your issue?
– Breeze Liu - MSFT
May 4 '18 at 2:00
Hi Breeze: see here: github.com/aherrick/XFormsOfflineSync Thank you!
– aherrick
May 4 '18 at 12:54
@aherrick I have modified my answer above, you can see it and have a try.
– Breeze Liu - MSFT
May 7 '18 at 4:43
I don't follow are you talking about this code? github.com/aherrick/XFormsOfflineSync/blob/master/… I have that code commented out
– aherrick
May 7 '18 at 13:01
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50126390%2fuwp-background-task-application-trigger-cannot-create-a-file-when-that-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
For your issue, it is because you have a file with the specified name already exists in the current folder when you create the file. You can try to specify the NameCollisionOption when you create the file.
var file = await ApplicationData.Current.LocalFolder.
CreateFileAsync("MyFile", CreationCollisionOption.ReplaceExisting);
As your code in your example project, you have used the ApplicationData.LocalSettings, when you add the setting key, you should check whether the key is existing.
var settings = Windows.Storage.ApplicationData.Current.LocalSettings;
if (!settings.Values.ContainsKey("BackgroundTask"))
{
settings.Values.Add("BackgroundTask", "Hello from UWP");
}
Or you can use the ApplicationData.LocalSettings as following to use the key and change its value,
var settings = Windows.Storage.ApplicationData.Current.LocalSettings;
settings.Values["BackgroundTask"] = "Hello from UWP";
You can also look into the Store and retrieve settings and other app data topic.
Thanks for the answer. However I'm seeing this behavior even after uninstall the UWP app completely and having VS redeploy. This is a Xamarin Forms app if that matters. It does skip over the "IsRegistered" call so it's definitely not registered on first load of a fresh app.
– aherrick
May 3 '18 at 17:49
I can not reproduce this issue in my side. I used the scenario in my answer, I will not get the error again. Could you provide a minimum sample to help me see your issue?
– Breeze Liu - MSFT
May 4 '18 at 2:00
Hi Breeze: see here: github.com/aherrick/XFormsOfflineSync Thank you!
– aherrick
May 4 '18 at 12:54
@aherrick I have modified my answer above, you can see it and have a try.
– Breeze Liu - MSFT
May 7 '18 at 4:43
I don't follow are you talking about this code? github.com/aherrick/XFormsOfflineSync/blob/master/… I have that code commented out
– aherrick
May 7 '18 at 13:01
|
show 3 more comments
For your issue, it is because you have a file with the specified name already exists in the current folder when you create the file. You can try to specify the NameCollisionOption when you create the file.
var file = await ApplicationData.Current.LocalFolder.
CreateFileAsync("MyFile", CreationCollisionOption.ReplaceExisting);
As your code in your example project, you have used the ApplicationData.LocalSettings, when you add the setting key, you should check whether the key is existing.
var settings = Windows.Storage.ApplicationData.Current.LocalSettings;
if (!settings.Values.ContainsKey("BackgroundTask"))
{
settings.Values.Add("BackgroundTask", "Hello from UWP");
}
Or you can use the ApplicationData.LocalSettings as following to use the key and change its value,
var settings = Windows.Storage.ApplicationData.Current.LocalSettings;
settings.Values["BackgroundTask"] = "Hello from UWP";
You can also look into the Store and retrieve settings and other app data topic.
Thanks for the answer. However I'm seeing this behavior even after uninstall the UWP app completely and having VS redeploy. This is a Xamarin Forms app if that matters. It does skip over the "IsRegistered" call so it's definitely not registered on first load of a fresh app.
– aherrick
May 3 '18 at 17:49
I can not reproduce this issue in my side. I used the scenario in my answer, I will not get the error again. Could you provide a minimum sample to help me see your issue?
– Breeze Liu - MSFT
May 4 '18 at 2:00
Hi Breeze: see here: github.com/aherrick/XFormsOfflineSync Thank you!
– aherrick
May 4 '18 at 12:54
@aherrick I have modified my answer above, you can see it and have a try.
– Breeze Liu - MSFT
May 7 '18 at 4:43
I don't follow are you talking about this code? github.com/aherrick/XFormsOfflineSync/blob/master/… I have that code commented out
– aherrick
May 7 '18 at 13:01
|
show 3 more comments
For your issue, it is because you have a file with the specified name already exists in the current folder when you create the file. You can try to specify the NameCollisionOption when you create the file.
var file = await ApplicationData.Current.LocalFolder.
CreateFileAsync("MyFile", CreationCollisionOption.ReplaceExisting);
As your code in your example project, you have used the ApplicationData.LocalSettings, when you add the setting key, you should check whether the key is existing.
var settings = Windows.Storage.ApplicationData.Current.LocalSettings;
if (!settings.Values.ContainsKey("BackgroundTask"))
{
settings.Values.Add("BackgroundTask", "Hello from UWP");
}
Or you can use the ApplicationData.LocalSettings as following to use the key and change its value,
var settings = Windows.Storage.ApplicationData.Current.LocalSettings;
settings.Values["BackgroundTask"] = "Hello from UWP";
You can also look into the Store and retrieve settings and other app data topic.
For your issue, it is because you have a file with the specified name already exists in the current folder when you create the file. You can try to specify the NameCollisionOption when you create the file.
var file = await ApplicationData.Current.LocalFolder.
CreateFileAsync("MyFile", CreationCollisionOption.ReplaceExisting);
As your code in your example project, you have used the ApplicationData.LocalSettings, when you add the setting key, you should check whether the key is existing.
var settings = Windows.Storage.ApplicationData.Current.LocalSettings;
if (!settings.Values.ContainsKey("BackgroundTask"))
{
settings.Values.Add("BackgroundTask", "Hello from UWP");
}
Or you can use the ApplicationData.LocalSettings as following to use the key and change its value,
var settings = Windows.Storage.ApplicationData.Current.LocalSettings;
settings.Values["BackgroundTask"] = "Hello from UWP";
You can also look into the Store and retrieve settings and other app data topic.
edited May 7 '18 at 4:51
answered May 3 '18 at 9:26
Breeze Liu - MSFTBreeze Liu - MSFT
2,9601511
2,9601511
Thanks for the answer. However I'm seeing this behavior even after uninstall the UWP app completely and having VS redeploy. This is a Xamarin Forms app if that matters. It does skip over the "IsRegistered" call so it's definitely not registered on first load of a fresh app.
– aherrick
May 3 '18 at 17:49
I can not reproduce this issue in my side. I used the scenario in my answer, I will not get the error again. Could you provide a minimum sample to help me see your issue?
– Breeze Liu - MSFT
May 4 '18 at 2:00
Hi Breeze: see here: github.com/aherrick/XFormsOfflineSync Thank you!
– aherrick
May 4 '18 at 12:54
@aherrick I have modified my answer above, you can see it and have a try.
– Breeze Liu - MSFT
May 7 '18 at 4:43
I don't follow are you talking about this code? github.com/aherrick/XFormsOfflineSync/blob/master/… I have that code commented out
– aherrick
May 7 '18 at 13:01
|
show 3 more comments
Thanks for the answer. However I'm seeing this behavior even after uninstall the UWP app completely and having VS redeploy. This is a Xamarin Forms app if that matters. It does skip over the "IsRegistered" call so it's definitely not registered on first load of a fresh app.
– aherrick
May 3 '18 at 17:49
I can not reproduce this issue in my side. I used the scenario in my answer, I will not get the error again. Could you provide a minimum sample to help me see your issue?
– Breeze Liu - MSFT
May 4 '18 at 2:00
Hi Breeze: see here: github.com/aherrick/XFormsOfflineSync Thank you!
– aherrick
May 4 '18 at 12:54
@aherrick I have modified my answer above, you can see it and have a try.
– Breeze Liu - MSFT
May 7 '18 at 4:43
I don't follow are you talking about this code? github.com/aherrick/XFormsOfflineSync/blob/master/… I have that code commented out
– aherrick
May 7 '18 at 13:01
Thanks for the answer. However I'm seeing this behavior even after uninstall the UWP app completely and having VS redeploy. This is a Xamarin Forms app if that matters. It does skip over the "IsRegistered" call so it's definitely not registered on first load of a fresh app.
– aherrick
May 3 '18 at 17:49
Thanks for the answer. However I'm seeing this behavior even after uninstall the UWP app completely and having VS redeploy. This is a Xamarin Forms app if that matters. It does skip over the "IsRegistered" call so it's definitely not registered on first load of a fresh app.
– aherrick
May 3 '18 at 17:49
I can not reproduce this issue in my side. I used the scenario in my answer, I will not get the error again. Could you provide a minimum sample to help me see your issue?
– Breeze Liu - MSFT
May 4 '18 at 2:00
I can not reproduce this issue in my side. I used the scenario in my answer, I will not get the error again. Could you provide a minimum sample to help me see your issue?
– Breeze Liu - MSFT
May 4 '18 at 2:00
Hi Breeze: see here: github.com/aherrick/XFormsOfflineSync Thank you!
– aherrick
May 4 '18 at 12:54
Hi Breeze: see here: github.com/aherrick/XFormsOfflineSync Thank you!
– aherrick
May 4 '18 at 12:54
@aherrick I have modified my answer above, you can see it and have a try.
– Breeze Liu - MSFT
May 7 '18 at 4:43
@aherrick I have modified my answer above, you can see it and have a try.
– Breeze Liu - MSFT
May 7 '18 at 4:43
I don't follow are you talking about this code? github.com/aherrick/XFormsOfflineSync/blob/master/… I have that code commented out
– aherrick
May 7 '18 at 13:01
I don't follow are you talking about this code? github.com/aherrick/XFormsOfflineSync/blob/master/… I have that code commented out
– aherrick
May 7 '18 at 13:01
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50126390%2fuwp-background-task-application-trigger-cannot-create-a-file-when-that-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown