Skip to content

Instantly share code, notes, and snippets.

@tetri
Forked from clupasq/timeout.cs
Last active April 23, 2019 17:33
Show Gist options
  • Save tetri/7a00c9e06410fc57fa0c8eba1da192c1 to your computer and use it in GitHub Desktop.
Save tetri/7a00c9e06410fc57fa0c8eba1da192c1 to your computer and use it in GitHub Desktop.
C# Timeout implementation
public void ComputeResultWithTimeout()
{
var task = Task.Factory.StartNew(() => ComputeResult("vrau"));
//task.Wait(0999);
task.Wait(1000);
if (task.IsCompleted)
Console.WriteLine($"oba! {task.Result}");
else
Console.WriteLine("oh não! tempo esgotado...");
}
public int ComputeResult(string someParameter)
{
Thread.Sleep(1000);
return 42;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment