Created
November 19, 2017 20:22
-
-
Save luisdeol/0161b02282ed4799b321fb5fbac7075a to your computer and use it in GitHub Desktop.
Using the TimeOut parameter of the Task.WaitAny overload
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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