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.

TaskExtensions.cs 2.4 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using MQTTnet.Exceptions;
  5. namespace MQTTnet.Internal
  6. {
  7. public static class TaskExtensions
  8. {
  9. public static async Task TimeoutAfter(this Task task, TimeSpan timeout)
  10. {
  11. if (task == null) throw new ArgumentNullException(nameof(task));
  12. using (var timeoutCts = new CancellationTokenSource())
  13. {
  14. try
  15. {
  16. var timeoutTask = Task.Delay(timeout, timeoutCts.Token);
  17. var finishedTask = await Task.WhenAny(timeoutTask, task).ConfigureAwait(false);
  18. if (finishedTask == timeoutTask)
  19. {
  20. throw new MqttCommunicationTimedOutException();
  21. }
  22. if (task.IsCanceled)
  23. {
  24. throw new TaskCanceledException();
  25. }
  26. if (task.IsFaulted)
  27. {
  28. throw new MqttCommunicationException(task.Exception.GetBaseException());
  29. }
  30. }
  31. finally
  32. {
  33. timeoutCts.Cancel();
  34. }
  35. }
  36. }
  37. public static async Task<TResult> TimeoutAfter<TResult>(this Task<TResult> task, TimeSpan timeout)
  38. {
  39. if (task == null) throw new ArgumentNullException(nameof(task));
  40. using (var timeoutCts = new CancellationTokenSource())
  41. {
  42. try
  43. {
  44. var timeoutTask = Task.Delay(timeout, timeoutCts.Token);
  45. var finishedTask = await Task.WhenAny(timeoutTask, task).ConfigureAwait(false);
  46. if (finishedTask == timeoutTask)
  47. {
  48. throw new MqttCommunicationTimedOutException();
  49. }
  50. if (task.IsCanceled)
  51. {
  52. throw new TaskCanceledException();
  53. }
  54. if (task.IsFaulted)
  55. {
  56. throw new MqttCommunicationException(task.Exception.GetBaseException());
  57. }
  58. return task.Result;
  59. }
  60. finally
  61. {
  62. timeoutCts.Cancel();
  63. }
  64. }
  65. }
  66. }
  67. }