If you expect your tasks to be completed within the predicted time frame, you can use timeouts.
Task.Wait has a couple of overloads that take a timeout value.
If, for example, your task should not take more than 5 seconds, you can do something like this.
var delayTask = DelayAsync(); // Will be true if DelayAsync() completes within 5 seconds, otherwise false. bool callCompleted = delayTask.Wait(TimeSpan.FromSeconds(5)); if (!callCompleted) { throw new TimeoutException("Task not completed within expected time."); }
source share