/// <summary> /// Immediately runs a background task. /// </summary> /// <param name="backgroundTask">The task to run.</param> public void Run(IBackgroundTask backgroundTask) { async Task TaskCallback(CancellationToken cancellationToken) { try { await backgroundTask.Run(cancellationToken); } catch { // TODO: Log details } } HostingEnvironment.QueueBackgroundWorkItem(TaskCallback); }
public void Run(IBackgroundTaskInstance taskInstance) { // Get a deferral so that the service isn't terminated. this.backgroundTaskDeferral = taskInstance.GetDeferral(); // Associate a cancellation handler with the background task. taskInstance.Canceled += OnTaskCanceled; // Retrieve the app service connection and set up a listener for incoming app service requests. var details = taskInstance.TriggerDetails as AppServiceTriggerDetails; appServiceconnection = details.AppServiceConnection; appServiceconnection.RequestReceived += OnRequestReceived; task = new StockBackgroundTask(); task.Run(taskInstance); }
/* * If each type of background task had only a single instance then it would make sense to use * background tasks created with IHostedService/BackgroundService in the web host. However, with * multiple copies of each background task it is easier to just use Task.Run */ static async Task RunTaskAfterDelay(IBackgroundTask t) { await Task.Delay(500); await t.Run(); }