TestScheduler.cs 715 B

123456789101112131415161718192021222324252627282930313233343536
  1. using System.Collections.Concurrent;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. namespace MonoTests {
  5. public class TestScheduler : TaskScheduler {
  6. readonly ConcurrentQueue<Task> queue = new ConcurrentQueue<Task> ();
  7. protected override void QueueTask (Task task)
  8. {
  9. queue.Enqueue (task);
  10. }
  11. protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
  12. {
  13. queue.Enqueue (task);
  14. return false;
  15. }
  16. protected override IEnumerable<Task> GetScheduledTasks ()
  17. {
  18. return queue;
  19. }
  20. public int ExecuteAll ()
  21. {
  22. int i = 0;
  23. Task task;
  24. while (queue.TryDequeue (out task)) {
  25. TryExecuteTask (task);
  26. i++;
  27. }
  28. return i;
  29. }
  30. }
  31. }