ARTICLE AD BOX
I have a problem:
i have a pool of tasks, which must be process within the time period until cancellation token is cancelled.
The rum must not last longer then defined time, otherwise es must be cancelled. Unfortunately, i have noticed that the set delay time is not being maintained.
Hier is code snippet:
private async Task DoStart(bool isMonitor) { try { var changeState = true; _socketTokenSource.Token.ThrowIfCancellationRequested(); while (!_socketTokenSource.Token.IsCancellationRequested) { await DoStartIntern(isMonitor); changeState = false; } } catch { } finally { _socketTokenSource.Dispose(); _socketTokenSource = new CancellationTokenSource(); } } private async Task DoStartIntern(bool isMonitor) { using var timeoutDataTokenSource = new CancellationTokenSource(); Task runTask = Task.Run(async () => { await DoProduceData(timeoutDataTokenSource.Token); var statusTask = !isMonitor ? DoProduceStatusData(timeoutDataTokenSource.Token) : DoProduceRemainingData(timeoutDataTokenSource.Token); await statusTask; }, timeoutDataTokenSource.Token); try { _unixTimeInMsec = ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeMilliseconds(); if (isMonitor) timeoutDataTokenSource.CancelAfter(200); await runTask; } catch(OperationCanceledException ex) { if (ex.CancellationToken != timeoutDataTokenSource.Token) throw new OperationCanceledException(); } } }Any ideas why it’s not working? Thank you very much.
