Skip to content

Instantly share code, notes, and snippets.

@gaevoy
Created October 19, 2016 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaevoy/b5b3cedac430960279283543040def84 to your computer and use it in GitHub Desktop.
Save gaevoy/b5b3cedac430960279283543040def84 to your computer and use it in GitHub Desktop.
[TestFixture]
public class WatcherTests
{
[Test]
public async Task ItShouldCollectWatcher()
{
var _ = new Watcher();
GC.Collect();
GC.Collect();
await Task.Delay(5000);
}
public class Watcher
{
private readonly CancellationTokenSource _tokenSource = new CancellationTokenSource();
public Watcher()
{
Watch(_tokenSource.Token);
}
private async void Watch(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
try
{
await Task.Delay(1000, cancellationToken);
Console.WriteLine("I am still alive");
}
catch (OperationCanceledException)
{
}
}
}
~Watcher()
{
_tokenSource.Cancel();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment