public static BackgroundTagging StartBackgroundTagging(PXColorizerTaggerBase tagger) { tagger.ThrowOnNull(nameof(tagger)); BackgroundTagging backgroundTagging = new BackgroundTagging(); backgroundTagging.TaggingTask = tagger.GetTagsAsyncImplementation(tagger.Snapshot, backgroundTagging.CancellationToken); backgroundTagging.TaggingTask?.ConfigureAwait(false); backgroundTagging.TaggingTask.ContinueWith(task => tagger.RaiseTagsChanged(), backgroundTagging.CancellationToken, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Default) .ConfigureAwait(false); return(backgroundTagging); }
public static BackgroundTagging StartBackgroundTagging(PXColorizerTaggerBase tagger) { tagger.ThrowOnNull(nameof(tagger)); BackgroundTagging backgroundTagging = new BackgroundTagging(); var taggingTask = tagger.GetTagsAsyncImplementationAsync(tagger.Snapshot, backgroundTagging.CancellationToken); if (taggingTask == null) { return(backgroundTagging); } // Use VS task scheduler from the VS synchronization context to schedule task continuation immediately to main thread // No need for synchronziation because FromCurrentSynchronizationContext creates schedulers which wrap around the same synchronization context // Therefore all schedulers should be identical and nothing wrong will happen if multiple threads create will create a scheduler _vsTaskScheduler = _vsTaskScheduler ?? TaskScheduler.FromCurrentSynchronizationContext(); backgroundTagging.TaggingTask = taggingTask.ContinueWith(task => AfterTaggingActionAsync(tagger, backgroundTagging.CancellationToken), //continuation should be on the UI thread backgroundTagging.CancellationToken, TaskContinuationOptions.OnlyOnRanToCompletion, _vsTaskScheduler); return(backgroundTagging); }