/// <summary> /// Runs <paramref name="idleWork"/> over each item returned from <paramref name="tryGetNextItem"/> on the UI Thread during idle time. /// This method will keep processing data from <paramref name="tryGetNextItem" /> on the same idle loop while VS remains idle. /// Once VS is no longer idle, the method will yield control and pick up where it left off on the next idle loop. /// </summary> /// <typeparam name="T">The type of the data to be processed.</typeparam> /// <param name="this">The IVsTaskSchedulerService to use when scheduling the work.</param> /// <param name="idleWork">Action to run on idle.</param> /// <param name="tryGetNextItem">Delegate used to get the next piece of data to process. tryGetNextItem should return true /// when another item is available and false when there is no more data to process.</param> /// <param name="token">Cancellation token to indicate when the work is no longer needed.</param> /// <returns> /// >A task representing all of the asynchronous work this method is performing. When the task is marked as completed, /// either all of the work received from tryGetNextItem has been processed, or the operation was canceled. /// </returns> public static async Task RunOnIdleAsync <T>(this IVsTaskSchedulerService @this, Action <T> idleWork, TryGetNextItem <T> tryGetNextItem, CancellationToken token) { // Check for good data if (idleWork == null) { throw new ArgumentNullException(nameof(idleWork)); } if (tryGetNextItem == null) { throw new ArgumentNullException(nameof(tryGetNextItem)); } Stopwatch stopwatch = new Stopwatch(); T data; while (tryGetNextItem(out data)) { token.ThrowIfCancellationRequested(); await VsTaskLibraryHelper.CreateAndStartTask( @this, VsTaskRunContext.UIThreadIdlePriority, VsTaskLibraryHelper.CreateTaskBody(() => { stopwatch.Start(); do { // Ensure cancellation occurring between work items is respected token.ThrowIfCancellationRequested(); idleWork(data); }while (stopwatch.ElapsedMilliseconds < MaxMillisecondIdleTime && tryGetNextItem(out data)); stopwatch.Stop(); })); stopwatch.Reset(); } }
/// <summary> /// Runs <paramref name="idleWork"/> over each item returned from <paramref name="tryGetNextItem"/> on the UI Thread during idle time. /// This method will keep processing data from <paramref name="tryGetNextItem" /> on the same idle loop while VS remains idle. /// Once VS is no longer idle, the method will yield control and pick up where it left off on the next idle loop. /// </summary> /// <typeparam name="T">The type of the data to be processed.</typeparam> /// <param name="this">The IVsTaskSchedulerService to use when scheduling the work.</param> /// <param name="idleWork">Action to run on idle.</param> /// <param name="tryGetNextItem">Delegate used to get the next piece of data to process. tryGetNextItem should return true /// when another item is available and false when there is no more data to process.</param> /// <returns> /// >A task representing all of the asynchronous work this method is performing. When the task is marked as completed, /// either all of the work received from tryGetNextItem has been processed, or the operation was canceled. /// </returns> public static Task RunOnIdleAsync <T>(this IVsTaskSchedulerService @this, Action <T> idleWork, TryGetNextItem <T> tryGetNextItem) { return(@this.RunOnIdleAsync <T>(idleWork, tryGetNextItem, CancellationToken.None)); }