Skip to content

Instantly share code, notes, and snippets.

@sandermvanvliet
Last active March 8, 2019 12:58
Show Gist options
  • Save sandermvanvliet/87521cb13ee82069cbd5a62a1b446a94 to your computer and use it in GitHub Desktop.
Save sandermvanvliet/87521cb13ee82069cbd5a62a1b446a94 to your computer and use it in GitHub Desktop.
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Jedlix.Assets.Application.API
{
public class Demo
{
public void Foo()
{
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1));
var task = Task.Factory.StartNew(LongRunningApiCall, cts.Token);
try
{
task.GetAwaiter().GetResult();
}
catch (TaskCanceledException)
{
// ignore or whatever
}
}
private void LongRunningApiCall()
{
// Do something stupid here
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment