public CancellationTokenSource StartSampling(int samplingInterval) { var tokenSource = new CancellationTokenSource(); CancellationToken ct = tokenSource.Token; var task = Task.Factory.StartNew(async() => { ct.ThrowIfCancellationRequested(); while (true) { var data = await _dataGetter.GetDataAsync(); var parsedData = await _parser.ParseAsync(data); foreach (var item in parsedData) { if (await IsValueChanged(item)) { await _dataSaver.SaveStockInfo(item); } } if (ct.IsCancellationRequested) { ct.ThrowIfCancellationRequested(); } await Task.Delay(samplingInterval * 1000, ct); } }, tokenSource.Token); return(tokenSource); }