public TaskExtender <byte[]> AddToDownload(string url, bool removeOnComplete) { //if url is not in queue, than create new task if (_tasks.Any(t => t.TaskName == url)) { return(_tasks.First(t => t.TaskName == url)); } TaskExtender <byte[]> newTask; if (_lastInQueue != null) { //if task is not first in queue, than create this task as continuation for previous one newTask = _lastInQueue.ContinueWith(nxt => { return(_fileDownloader.Download(url)); }, url, removeOnComplete ); } else { //this is first task in queue, just add it to the queue newTask = new TaskExtender <byte[]>(() => { return(_fileDownloader.Download(url)); }, url, removeOnComplete); } newTask.OnTaskCompleted += InnerCompletion; _tasks.Enqueue(newTask); _lastInQueue = newTask; return(_lastInQueue); }
/// <summary> /// Allows to set Func as continuation to current TaskExtender; /// </summary> /// <param name="func">func to create the task</param> /// <param name="taskName">task name</param> /// <param name="removeOnCompleted">remove extender from queue on completion</param> /// <returns></returns> public TaskExtender <T> ContinueWith <T>(Func <object, T> func, string taskName, bool removeOnCompleted) { Task <T> newTask = this.Task.ContinueWith(func); var newExtender = new TaskExtender <T>(newTask, taskName, removeOnCompleted); newExtender.TaskName = taskName; return(newExtender); }