Windowscommunitytoolkit: Why can't we register background tasks with typeof in Single Process Model?

Created on 13 Feb 2017  路  17Comments  路  Source: windows-toolkit/WindowsCommunityToolkit

I tried to use the BackgroundTaskHelper which is a really great.

I have one question regarding the implementation of the method in Single Process Model :

  • Why can't we use the typeof like we can do for the normal Register method?
  • I think we can be confused between the two Register methods (with/without SPM), why not renaming it to RegisterInSingleProcess or something similar?
bug question services

All 17 comments

Ping @Code-ScottLe

I had a similar discussion with @anbare when the PR was still going. You can see it here.

But to answer your questions directly:

  1. We can, because SPM requires a task name only and task name can be anything.
  2. I think the decision of not having a RegisterInSPM was the confusion of having both Register and RegisterInSPM (Register can do both SPM and MPM, while RegisterInSPM can only do SPM )

@Code-ScottLe I asked 2) because it is related to 1). I mean I can't create the Register method with a Type for SPM because there is already a method with the same arguments that is used for MPM. Or maybe the comments are wrong?

I think there were a slight misunderstanding here.

It is correct that the overload for Register that use Type is currently being used only for registering MPM and not SPM. And from the technical stand point, we can definitely use Type for registering SPM. But by doing this, we have to create a new function, with a new name (can't be another overload for Register because the current overload with Type parameter is already being used to register MPM) and it comes back to my 2. point (confusion with different functions).

I'm not against adding in another wrapper at all, I'll add it we all agree it is a good idea :)

@Code-ScottLe Thanks for the clarification. Like I said, I would like a method with Type to register Background Task in SPM without breaking changes and removing any confusion. We'll discuss with other.

I agree with @Odonno

@Code-ScottLe Ok, now I have a better understanding of BackgroundTask in Single Process Model. I am frustrated that there is no good documentation. I had to check on 10 different websites to collect enough information to understand the behavior of BackgroundTask in SPM and the differences with Parallel Process Model.

Here are the facts/conclusions I made :

  • I tried to use the Register (with SPM) method provided by the Toolkit but it (Background Task) never worked / background task never executed (using debugger or in real live situation)
  • But the Register method is well written because the event OnBackgroundActivated in App is executed (that's the entry point of Background Task in SPM)
  • I assume the code that was written does not work for SPM completely and I found a solution
  • Overriding the OnBackgroundActivated with the following process :
    1) Use GetDeferral to start the background process
    2) Execute Background Task like you normally do by execute the Run method, the only way I found is to instanciate a new instance of the BackgroundTask class with Activator but I need both the namespace and the name of the class to instanciate the object
    3) Finish the process using Complete method
    4) Optional but we can support Exception and Cancellation of the process

I am able to submit a PR to fix/complete the feature. Here is a summary of what is required :

  • Documentation to explain that we have to override OnBackgroundActivated to handle BackgroundTask in SPM
  • Implementation of a helper method to HandleActivationInSPM to execute a BackgroundTask by providing both the namespace and the class name (remember that we can pass the Task name from args of OnBackgroundActivated method)

What do you think?

@Code-ScottLe Otherwise, regarding the implementation of Register method, I found it is ok to have only a string argument and not Type because we can register a BackgroundTask in SPM no matter what the name is. For example, It could be The name of my awesome Background Task in SPM.

@Odonno SPM requires you to be on 14393 SDK , the Sample app of this toolkit is targeting 10586, can you verify that your test app is targeting 14393 at the minimum?

I will get back to you on this. But having to override the BackgroundActivated event seems a little weird to me.

@Code-ScottLe I was not using the Sample app but one of my own app. Targeting minimum SDK 14393 does not change anything.

One fact is that my class is not on the default namespace of the app but in the sub-namespace (like MyNamespace.MyTasks). Maybe, maybe not. Like I said, the docs are not really user-friendly.

Using Activator in OnBackgroundActivated is the only way I found. And it makes sense to me, that's the entry point for handling background tasks or any other activities in background. It's like the Activated event. You have flexibility to things the way you want. I understand it like that.

I hope you'll find better explanation than mine.

By the way, here is the code I use to execute Background Task in SPM :

protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args)
{
    base.OnBackgroundActivated(args);

    var deferral = args.TaskInstance.GetDeferral();

    var task = Activator.CreateInstance(Type.GetType("MyApp.BackgroundTasks." + args.TaskInstance.Task.Name)) as IBackgroundTask;
    task.Run(args.TaskInstance);

    deferral.Complete();
}

Yep, that's the correct code snippet for SPM. You simply override that method in your App.xaml.cs file. You have to be targeting SDK 14393 or higher.

Then, when you register a background task, if you don't specify an entry point, the OnBackgroundActivated will be triggered.

It doesn't make sense to have an overload that only takes a Type for SPM. What would the type even be? This isn't like the normal MPM where you have to have a dedicated class for your background task... for SPM, all background tasks get triggered within your App class. So there's no type to pass. That's why you have to provide a string for the name.

@anbare Check my PR #965

I think people may have misunderstand how to use Background Task in SPM. And you can count me in.

The sample app is using SPM so I started a PR to fix, I mean show how to use it. Then, I will update the docs so anyone can clearly understand how to do.

I thought we were overriding the BackgroundActivated Event itself and not the Event Handler OnBackgroundActivated , my bad there. But overriding the function OnBackgroundActivated is the correctly way to handle SPM background task.

And with the Type thing, the only convenience that you get out of that for SPM is not having to maintain a name string, but that could be easily solved by using nameof keyword .

@Code-ScottLe No worry. Let's fix that so anyone after me do not have to search the info.

And thanks for the tip, I did not think of nameof but that is a really good solution.

There is a problem that I can't solve alone. I want to provide a way for people to do as less code as possible to handle Background Tasks in SPM, but in the same time, we need to let them some flexibility.

@Odonno I also had that question when i first implemented this API, but then after the discussion on the original PR, I ended up with this overload:
public static BackgroundTaskRegistration Register(string backgroundTaskName, IBackgroundTrigger trigger, bool forceRegister = false, bool enforceConditions = true, params IBackgroundCondition[] conditions)

Technically, this one was designed to register SPM-only background task (hence the missing string for taskEntryPoint) and anyone who did a read on SPM would know about the lack of task entry point to recognize this overload. With the combination of nameof keyword, I thought it was clean enough. Keep in mind, the name of the background task can be anything, I just like to use the name of some typed class so I can easily relate to and not having to worry about maintaining some constant string.

Closing it as we updated the sample app

Was this page helpful?
0 / 5 - 0 ratings