BackgroundTaskBuilder.Register() throws System.ArgumentException "Value does not fall within..."

Hong 1,436 Reputation points
2021-02-02T21:07:14.897+00:00

I copied the following code from a Microsoft official document.

BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync();  
  
// Otherwise request access  
BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync();  
  
// Create the background task  
BackgroundTaskBuilder builder = new BackgroundTaskBuilder()  
{  
    Name = Utility.sOpenImageFileTask,  
};  
  
// Assign the toast action trigger  
builder.SetTrigger(new ToastNotificationActionTrigger());  
  
// And register the task  
BackgroundTaskRegistration registration = builder.Register();  

It worked for a while. I suddenly started to see the following exception thrown by builder.Register().

System.ArgumentException Value does not fall within the expected range.
Stack Trace: at System.Runtime.InteropServices.McgMarshal.ThrowOnExternalCallFailed(Int32, RuntimeTypeHandle) + 0x21
at __Interop.ComCallHelpers.Call(__ComObject, RuntimeTypeHandle, Int32, Void*) + 0xbe
at __Interop.ForwardComStubs.Stub_3TThis, TResult + 0x37
at Windows.ApplicationModel.Background.BackgroundTaskBuilder.Register() + 0x18

Could anyone offer a tip on the possible causes of this exception?

Developer technologies | Universal Windows Platform (UWP)
{count} votes

Answer accepted by question author
  1. Yan Gu - MSFT 2,676 Reputation points
    2021-02-04T02:41:47.86+00:00

    Hello,

    Welcome to Microsoft Q&A.

    The System.ArgumentException is reproduced when I add the multiple-instance support. And I also find the location of the exception.

    You could refer to Background tasks and multi-instancing part of the Create a multi-instance Universal Windows App, and get the information that out-of-process background tasks support multi-instancing and in-process background tasks do not support multi-instancing.

    In my side, when I register an in-process background task, the System.ArgumentException is thrown. When I register an out-of-process background task instead of the in-process background task, the app could run successfully.

    Based on the code provided by you, it should be an in-process background task because BackgroundTaskBuilder.TaskEntryPoint Property is not set for the BackgroundTaskBuilder instance. You could try to replace your in-process background task with an out-of-process background task.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.