UWP Background Task - Application Trigger - Cannot create a file when that file already exists

2

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
asked on Stack Overflow May 2, 2018 by aherrick

2 Answers

-1

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.

answered on Stack Overflow May 3, 2018 by Breeze Liu - MSFT • edited May 7, 2018 by Breeze Liu - MSFT
-1

I'm using ApplicationTrigger for the BackgroundTask registration, and creating a new instance of ApplicationTrigger before re-registering fixed this error for me.

answered on Stack Overflow Feb 14, 2019 by ss9326

User contributions licensed under CC BY-SA 3.0