public async void Run(IBackgroundTaskInstance taskInstance) { backgroundTaskInstance = taskInstance; // Trigger details contain device id and arguments that are provided by the caller in the main app // The taskInstance can always be casted to DeviceServicingDetails if this background task was started using a DeviceServicingTrigger deviceServicingDetails = (DeviceServicingDetails)taskInstance.TriggerDetails; deferral = taskInstance.GetDeferral(); try { backgroundTaskInstance.Progress = 0; cancellationTokenSource = new CancellationTokenSource(); taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled); // For simplicity, no error checking will be done after opening the device. Ideally, one should always // check if the device was successfully opened and respond accordingly. For an example on how to do this, // please see Scenario 1 of this sample. await OpenDeviceAsync(); // We opened the device, so notify the app that we've completed a bit of the background task backgroundTaskInstance.Progress = 10; // Setup the device for firmware update await SetupDeviceForFirmwareUpdateAsync(); // Write firmware to the device UInt32 totalBytesWrittenForFirmware = await WriteFirmwareAsync(); // Reset the device so the device loads the updated firmware await ResetDeviceAsync(); // The device was closed during reset, so clean up our UsbDevice object CloseDevice(); // Wait for the device to be reenumerated. A device watcher can also be used instead to watch for the device to be reenumerated; that way // you know when the device is opened instead of guessing like we do here await Task.Delay(4000); // We've completed firmware update backgroundTaskInstance.Progress = 100; // Reopen the device so we can grab the new revision number because the revision number reflects the firmware verson of the SuperMutt device. // A consent prompt would normally appear here because we updated the device (revision number was changed in this case), but the Firmware Update API // allows the background task to open any device that is listed in the device metadata file (the one that allowed this firmware update background task to start) // without a consent prompt. The consent prompt will still appear if the same device is opened outside the background task. await OpenDeviceAsync(); var newFirmwareVersion = device.DeviceDescriptor.BcdDeviceRevision; CloseDevice(); ApplicationData.Current.LocalSettings.Values[LocalSettingKeys.FirmwareUpdateBackgroundTask.TaskStatus] = FirmwareUpdateTaskInformation.TaskCompleted; ApplicationData.Current.LocalSettings.Values[LocalSettingKeys.FirmwareUpdateBackgroundTask.NewFirmwareVersion] = newFirmwareVersion; } catch (OperationCanceledException /*ex*/) { ApplicationData.Current.LocalSettings.Values[LocalSettingKeys.FirmwareUpdateBackgroundTask.TaskStatus] = FirmwareUpdateTaskInformation.TaskCanceled; } finally { // Complete the background task (this raises the OnCompleted event on the corresponding BackgroundTaskRegistration) deferral.Complete(); } }