public static void QueueBackgroundWorkItemPf(Func<CancellationToken, Task> workItem) { BackgroundWorkSchedulerPf scheduler = Volatile.Read(ref _backgroundWorkScheduler); // If the scheduler doesn't exist, lazily create it, but only allow one instance to ever be published to the backing field if (scheduler == null) { BackgroundWorkSchedulerPf newlyCreatedScheduler = new BackgroundWorkSchedulerPf(HostingEnvironment.UnregisterObject); scheduler = Interlocked.CompareExchange(ref _backgroundWorkScheduler, newlyCreatedScheduler, null) ?? newlyCreatedScheduler; if (scheduler == newlyCreatedScheduler) { HostingEnvironment.RegisterObject(scheduler); // Only call RegisterObject if we just created the "winning" one } } scheduler.ScheduleWorkItem(workItem); }
public static void QueueBackgroundWorkItemPf(Func <CancellationToken, Task> workItem) { BackgroundWorkSchedulerPf scheduler = Volatile.Read(ref _backgroundWorkScheduler); // If the scheduler doesn't exist, lazily create it, but only allow one instance to ever be published to the backing field if (scheduler == null) { BackgroundWorkSchedulerPf newlyCreatedScheduler = new BackgroundWorkSchedulerPf(HostingEnvironment.UnregisterObject); scheduler = Interlocked.CompareExchange(ref _backgroundWorkScheduler, newlyCreatedScheduler, null) ?? newlyCreatedScheduler; if (scheduler == newlyCreatedScheduler) { HostingEnvironment.RegisterObject(scheduler); // Only call RegisterObject if we just created the "winning" one } } scheduler.ScheduleWorkItem(workItem); }