// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; using System.Threading; using System.Threading.Tasks; namespace ICSharpCode.AddInManager2 { public class AddInManagerTask { public static AddInManagerTask Create( Func function, Action> continueWith) { return new AddInManagerTask(function, continueWith); } } public class AddInManagerTask { Task task; Action> continueWith; CancellationTokenSource cancellationTokenSource; public AddInManagerTask( Func function, Action> continueWith) { this.continueWith = continueWith; CreateTask(function); } private void CreateTask(Func function) { TaskScheduler scheduler = TaskScheduler.FromCurrentSynchronizationContext(); cancellationTokenSource = new CancellationTokenSource(); task = new Task(function, cancellationTokenSource.Token); task.ContinueWith(result => OnContinueWith(result), scheduler); } private void OnContinueWith(Task task) { continueWith(this); } public void Start() { task.Start(); } public TResult Result { get { return task.Result; } } public void Cancel() { cancellationTokenSource.Cancel(); } public bool IsCancelled { get { return cancellationTokenSource.IsCancellationRequested; } } public bool IsFaulted { get { return task.IsFaulted; } } public AggregateException Exception { get { return task.Exception; } } } }