Skip to content

Instantly share code, notes, and snippets.

@james-world
Created October 8, 2014 08:43
Show Gist options
  • Save james-world/ee4d9c04765263070671 to your computer and use it in GitHub Desktop.
Save james-world/ee4d9c04765263070671 to your computer and use it in GitHub Desktop.
How to watch a bad task and not fall into the cancellation token trap!
using System;
using System.Reactive.Linq;
using System.Reactive.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;
namespace AsyncEvil
{
class Program
{
static void Main(string[] args)
{
var evt = new AutoResetEvent(false);
var task = Task.Run(() =>
{
evt.WaitOne();
});
task.ToObservable()
.Timeout(TimeSpan.FromSeconds(5))
.Subscribe(_ => { }, ex => Console.WriteLine(ex.Message));
Console.WriteLine("Waiting...");
Console.ReadKey(true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment