Skip to content

Instantly share code, notes, and snippets.

@benrr101
Last active December 19, 2021 05:12
Show Gist options
  • Save benrr101/199b3367956f3c2c5de5e2d5788d4f7e to your computer and use it in GitHub Desktop.
Save benrr101/199b3367956f3c2c5de5e2d5788d4f7e to your computer and use it in GitHub Desktop.
public static async Task<Task> WithTimeout(this Task task, TimeSpan timeout)
{
Task delayTask = Task.Delay(TimeSpan.FromSeconds(1));
Task firstCompleted = await Task.WhenAny(task, delayTask);
if (firstCompleted == delayTask)
{
throw new Exception("Task timed out");
}
return task;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment