GodotSynchronizationContext.cs 742 B

12345678910111213141516171819202122232425
  1. using System.Collections.Concurrent;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. namespace Godot
  5. {
  6. public class GodotSynchronizationContext : SynchronizationContext
  7. {
  8. private readonly BlockingCollection<KeyValuePair<SendOrPostCallback, object>> _queue =
  9. new BlockingCollection<KeyValuePair<SendOrPostCallback, object>>();
  10. public override void Post(SendOrPostCallback d, object state)
  11. {
  12. _queue.Add(new KeyValuePair<SendOrPostCallback, object>(d, state));
  13. }
  14. public void ExecutePendingContinuations()
  15. {
  16. while (_queue.TryTake(out var workItem))
  17. {
  18. workItem.Key(workItem.Value);
  19. }
  20. }
  21. }
  22. }