GodotSynchronizationContext.cs 678 B

1234567891011121314151617181920212223242526
  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 = new BlockingCollection<KeyValuePair<SendOrPostCallback, object>>();
  9. public override void Post(SendOrPostCallback d, object state)
  10. {
  11. queue.Add(new KeyValuePair<SendOrPostCallback, object>(d, state));
  12. }
  13. public void ExecutePendingContinuations()
  14. {
  15. KeyValuePair<SendOrPostCallback, object> workItem;
  16. while (queue.TryTake(out workItem))
  17. {
  18. workItem.Key(workItem.Value);
  19. }
  20. }
  21. }
  22. }