ListBasedUpdateSynchronizationContext.cs 934 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. namespace Urho
  5. {
  6. public class ListBasedUpdateSynchronizationContext : SynchronizationContext
  7. {
  8. readonly IList<Action> list;
  9. public ListBasedUpdateSynchronizationContext(IList<Action> list)
  10. {
  11. this.list = list;
  12. }
  13. public override SynchronizationContext CreateCopy()
  14. {
  15. return new ListBasedUpdateSynchronizationContext(list);
  16. }
  17. public override void Post(SendOrPostCallback d, object state)
  18. {
  19. lock (list)
  20. {
  21. list.Add(() => d(state));
  22. }
  23. }
  24. public override void Send(SendOrPostCallback d, object state)
  25. {
  26. //seems there is no difference between Post and Send for our case.
  27. lock (list)
  28. {
  29. list.Add(() => d(state));
  30. }
  31. }
  32. public void PumpActions()
  33. {
  34. List<Action> copy;
  35. lock (list)
  36. {
  37. copy = new List<Action>(list);
  38. list.Clear();
  39. }
  40. foreach (var action in copy)
  41. action();
  42. }
  43. }
  44. }