Skip to content

Instantly share code, notes, and snippets.

@luisdeol
Created November 19, 2017 20:22
Show Gist options
  • Save luisdeol/0161b02282ed4799b321fb5fbac7075a to your computer and use it in GitHub Desktop.
Save luisdeol/0161b02282ed4799b321fb5fbac7075a to your computer and use it in GitHub Desktop.
Using the TimeOut parameter of the Task.WaitAny overload
namespace manage_multithreading
{
class Program
{
static void Main(string[] args)
{
Task notThatPowerfulTask = Task.Run(() =>
{
Console.WriteLine("A long-running task is being executed right now...");
Thread.Sleep(10000);
});
Console.WriteLine("Well, I have only 5s...");
int result = Task.WaitAny(new[] {notThatPowerfulTask}, 5000);
if (result == -1)
Console.WriteLine("I am out... It is taking too much time!");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment