You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AsyncAutoResetEvent.cs 763 B

7 jaren geleden
1234567891011121314151617181920212223242526272829303132
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. namespace MQTTnet.Core.Internal
  4. {
  5. public sealed class AsyncGate
  6. {
  7. private readonly Queue<TaskCompletionSource<bool>> _waitingTasks = new Queue<TaskCompletionSource<bool>>();
  8. public Task WaitOneAsync()
  9. {
  10. var tcs = new TaskCompletionSource<bool>();
  11. lock (_waitingTasks)
  12. {
  13. _waitingTasks.Enqueue(tcs);
  14. }
  15. return tcs.Task;
  16. }
  17. public void Set()
  18. {
  19. lock (_waitingTasks)
  20. {
  21. if (_waitingTasks.Count > 0)
  22. {
  23. _waitingTasks.Dequeue().SetResult(true);
  24. }
  25. }
  26. }
  27. }
  28. }