Created
October 19, 2016 11:29
-
-
Save gaevoy/b5b3cedac430960279283543040def84 to your computer and use it in GitHub Desktop.
This file contains 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
[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