private void SpinAwait() { while (!disposing) { try { WaitForAvailableThread(); if (disposing) { return; } if (PendingTasks.Count > 0) { AbortableTask action = PendingTasks[0]; try { action.Task.Start(TaskFactory.Scheduler); Tasks.Add(action); } catch (System.Exception ex1) { if (OnThreadException != null) { OnThreadException.Invoke(this, ex1, action); } else { throw; } } PendingTasks.Remove(action); } } catch (System.Exception ex0) { if (OnThreadException != null) { OnThreadException.Invoke(this, ex0); } else { throw; } } System.Threading.Thread.Sleep(20); } }
/// <summary> /// Starts the <see cref="AbortableTask">Task</see> using <see cref="TaskManager.TaskFactory">TaskFactory</see> /// and adds it to the <see cref="TaskManager.Tasks">Tasks</see> collection or the /// <see cref="TaskManager.PendingTasks">PendingTasks</see> collection if no threads are available. /// </summary> /// <param name="task"></param> public void StartTask(AbortableTask task) { if (MaximumConcurrency <= RunningTasks.Count()) { PendingTasks.Add(task); } else { Tasks.Add(task); if (TaskFactory.Scheduler == null) { task.Task.Start(); } else { task.Task.Start(TaskFactory.Scheduler); } } }
/// <summary> /// Starts the <see cref="System.Threading.Tasks.Task">Task</see> using <see cref="TaskManager.TaskFactory">TaskFactory</see> /// and adds it to the <see cref="TaskManager.Tasks">Tasks</see> collection or the /// <see cref="TaskManager.PendingTasks">PendingTasks</see> collection if no threads are available /// using the provided <see cref="System.Threading.CancellationTokenSource">CancellationTokenSource</see>. /// </summary> /// <param name="task">The <see cref="System.Threading.Tasks.Task">Task</see> to be executed.</param> /// <param name="cancellationTokenSource">The <see cref="System.Threading.CancellationTokenSource">source</see> /// of the <see cref="System.Threading.CancellationToken">CancellationToken</see>.</param> public void StartTask(Task task, System.Threading.CancellationTokenSource cancellationTokenSource) { AbortableTask abt = new AbortableTask(task, cancellationTokenSource); StartTask(abt); }
/// <summary> /// Starts the <see cref="System.Threading.Tasks.Task">Task</see> using <see cref="TaskManager.TaskFactory">TaskFactory</see> /// and adds it to the <see cref="TaskManager.Tasks">Tasks</see> collection or the /// <see cref="TaskManager.PendingTasks">PendingTasks</see> collection if no threads are available. /// </summary> /// <param name="task">The <see cref="System.Threading.Tasks.Task">Task</see> to be executed.</param> public void StartTask(Task task) { AbortableTask abt = new AbortableTask(task); StartTask(abt); }