Show / Hide Table of Contents

Method RegisterForBGTaskServer

| Edit this page View Source

RegisterForBGTaskServer()

Registers the background task COM server with the application. Only call this if the program startup arguments contains -RegisterForBGTaskServer.

Declaration
public static void RegisterForBGTaskServer()
Remarks

For the JobManager to also run in the background, it must be registered as a background task COM server on application startup. In the Main startup method, add the following code:

static void Main(string[] args)
{
    if (args.Contains("-RegisterForBGTaskServer"))
    {
        JobManager.RegisterForBGTaskServer();
    }
    else
    {
        // Normal app startup code here
    }
}

For the job manager background task to work, the application manifest must also be configured to declare the background task using the COM server class ID "D998B238-C096-4181-B971-9CD1EB760547". Under the Application section add:

*lt;Extensions>
    *lt;!-- Below Entry Point is to be mentioned in case of C# application for usage of WinAppSDK Background Task API -->
    *lt;Extension Category="windows.backgroundTasks" EntryPoint="Microsoft.Windows.ApplicationModel.Background.UniversalBGTask.Task">
    *lt;BackgroundTasks>
        *lt;Task Type="general"/>
    *lt;/BackgroundTasks>
    *lt;/Extension>
    *lt;com:Extension Category="windows.comServer">
    *lt;com:ComServer>
        *lt;!-- COM Server for the background task, LaunchAndActivationPermission is required to give permission
     for backgroundtaskhost process to cocreate this COM component -->
        *lt;com:ExeServer Executable="MyApplicationName.exe" Arguments="-RegisterForBGTaskServer" DisplayName="BackgroundTask"
              LaunchAndActivationPermission="O:PSG:BUD:(A;;11;;;IU)(A;;11;;;S-1-15-2-1)S:(ML;;NX;;;LW)">
        *lt;com:Class Id="D998B238-C096-4181-B971-9CD1EB760547" DisplayName="BackgroundTask" />
        *lt;/com:ExeServer>
    *lt;/com:ComServer>
    *lt;/com:Extension>
*lt;/Extensions>

And under the <c>Package></c section add the following:

<Extensions>
    <Extension Category="windows.activatableClass.inProcessServer">
        <InProcessServer>
            <Path>Microsoft.Windows.ApplicationModel.Background.UniversalBGTask.dll</Path>
            <ActivatableClass ActivatableClassId="Microsoft.Windows.ApplicationModel.Background.UniversalBGTask.Task" ThreadingModel="both"/>
        </InProcessServer>
    </Extension>
</Extensions>
  • Edit this page
  • View Source
In this article
Back to top Generated by DocFX