/// <summary> Ends this task, firing the 100% complete event if necessary, and pops the Progress stack. /// </summary> /// <param name="completedSuccessfully"> /// Determines if the 100% complete event should be fired or skipped. /// </param> private void EndTask(bool completedSuccessfully) { // Only dispose once: if (this.isEnded) { return; } this.isEnded = true; // Report the 100% progress: if (completedSuccessfully) { this.OnProgressChanged(null); } // Report the progress Ended: if (this.progressEndedCallback != null) { this.progressEndedCallback(new ProgressChangedInfo(new ProgressInfo(100f, taskKey, taskArg), null)); this.progressEndedCallback = null; } // Clear events: this.progressChangedCallback = null; // Clear calculator: (dispose if possible) var calcDispose = this.calculator as IDisposable; if (calcDispose != null) { calcDispose.Dispose(); } this.calculator = null; // DEBUG: Make sure "this" is on the top of the stack. // This might not be the case if you fail to dispose/end a task, // or if you dispose it from a thread other than the one that created it. // Who would do such a thing?!? while (currentTask != this && currentTask != null) { Debug.Write("Warning: a Progress Task was not properly disposed."); currentTask.Dispose(); } Debug.Assert(currentTask != null, "A Progress Task must be disposed from the thread that created it."); // Pop the stack: currentTask = this.parent; }
/// <summary> Pushes a new task to the top of the stack. /// </summary> /// <param name="calculator"></param> public ProgressTask(IProgressCalculator calculator) { this.calculator = calculator; // Push this new task on the top of the task stack: this.parent = currentTask; currentTask = this; // Calculate the new maximum depth: if (this.parent == null) { this.maximumDepth = ProgressDepth.Auto; } else { this.maximumDepth = parent.maximumDepth - 1; } }
public NewExpenseViewModel(IDataStore dataStore, IProgressCalculator progressCalculator) : base(dataStore) { try { _progressCalculator = progressCalculator; var _task = _progressCalculator.CalculateProgress(); MessagingCenter.Subscribe <BudgetsViewModel>(this, Constants.CategoriesChanged, async(sender) => { Progress = new ObservableCollection <BudgetProgress>(await _progressCalculator.CalculateProgress()); }); MessagingCenter.Subscribe <ItemEditorViewModel>(this, Constants.ExpenseChanged, async(sender) => { Progress = new ObservableCollection <BudgetProgress>(await _progressCalculator.CalculateProgress()); }); Progress = new ObservableCollection <BudgetProgress>(_task.Result); }catch (Exception ex) { Console.WriteLine(ex.Message); throw; } }
/// <summary> Tracks progress as the source is enumerated. </summary> /// <param name="source"></param> /// <param name="calculator">A custom progress calculator</param> public static ProgressEnumerator <T> WithProgress <T>(this IEnumerable <T> source, IProgressCalculator calculator) { return(new ProgressEnumerator <T>(source, Progress.BeginCustomTask(calculator))); }
/// <summary> Ends this task, firing the 100% complete event if necessary, and pops the Progress stack. /// </summary> /// <param name="completedSuccessfully"> /// Determines if the 100% complete event should be fired or skipped. /// </param> private void EndTask(bool completedSuccessfully) { // Only dispose once: if (this.isEnded) return; this.isEnded = true; // Report the 100% progress: if (completedSuccessfully) { this.OnProgressChanged(null); } // Report the progress Ended: if (this.progressEndedCallback != null) { this.progressEndedCallback(new ProgressChangedInfo(new ProgressInfo(100f, taskKey, taskArg), null)); this.progressEndedCallback = null; } // Clear events: this.progressChangedCallback = null; // Clear calculator: (dispose if possible) var calcDispose = this.calculator as IDisposable; if (calcDispose != null) calcDispose.Dispose(); this.calculator = null; // DEBUG: Make sure "this" is on the top of the stack. // This might not be the case if you fail to dispose/end a task, // or if you dispose it from a thread other than the one that created it. // Who would do such a thing?!? while (currentTask != this && currentTask != null) { Debug.Write("Warning: a Progress Task was not properly disposed."); currentTask.Dispose(); } Debug.Assert(currentTask != null, "A Progress Task must be disposed from the thread that created it."); // Pop the stack: currentTask = this.parent; }
/// <summary> Starts a task using a custom progress calculator. </summary> /// <param name="calculator">Any custom progress calculator</param> /// <returns>Returns an object that ends the task when it is disposed.</returns> public static ProgressTask BeginCustomTask(IProgressCalculator calculator) { return new ProgressTask(calculator); }
/// <summary> Starts a task using a custom progress calculator. </summary> /// <param name="calculator">Any custom progress calculator</param> /// <returns>Returns an object that ends the task when it is disposed.</returns> public static ProgressTask BeginCustomTask(IProgressCalculator calculator) { return(new ProgressTask(calculator)); }