async Task RunnerAsync(RunnerStats stats, CancellationToken cancellationToken) { try { await this.sender.OpenAsync(); stats.IncrementDeviceConnected(); // Delay first event by a random amount to avoid bursts await Task.Delay(this.random.Next(this.config.Interval), cancellationToken); var stopwatch = new Stopwatch(); stopwatch.Start(); for (var i = 0L; !cancellationToken.IsCancellationRequested && (this.config.MessageCount <= 0 || i < this.config.MessageCount); i++) { await this.sender.SendMessageAsync(stats, cancellationToken); var millisecondsDelay = Math.Max(0, this.config.Interval * i - stopwatch.ElapsedMilliseconds); await Task.Delay((int)millisecondsDelay, cancellationToken); } stats.IncrementCompletedDevice(); } catch (OperationCanceledException) when(cancellationToken.IsCancellationRequested) { } catch (Exception ex) { Console.Error.WriteLine(ex); } }
async Task RunnerAsync(RunnerStats stats, CancellationToken cancellationToken) { try { await this.sender.OpenAsync(); stats.IncrementDeviceConnected(); for (var i = 0; !cancellationToken.IsCancellationRequested && (this.config.MessageCount <= 0 || i < this.config.MessageCount); i++) { await Task.Delay(this.config.Interval, cancellationToken); await this.sender.SendMessageAsync(stats, cancellationToken); } stats.IncrementCompletedDevice(); } catch (OperationCanceledException) when(cancellationToken.IsCancellationRequested) { } catch (Exception ex) { Console.Error.WriteLine(ex); } }
async Task RunnerAsync(RunnerStats stats, CancellationToken cancellationToken) { try { await this.sender.OpenAsync(); stats.IncrementDeviceConnected(); // Delay first event by a random amount to avoid bursts int currentInterval = this.interval[0]; await Task.Delay(this.random.Next(currentInterval), cancellationToken); var stopwatch = new Stopwatch(); stopwatch.Start(); long totalIntervalTime = 0; var messageCount = (ulong)this.config.MessageCount; for (ulong i = 0; !cancellationToken.IsCancellationRequested && (messageCount == 0 || i < messageCount); i++) { if (i % 1000 == 0) { totalIntervalTime = 0; stopwatch.Restart(); } await this.sender.SendMessageAsync(stats, cancellationToken); currentInterval = this.interval[i % (ulong)this.interval.Length]; totalIntervalTime += currentInterval; var millisecondsDelay = Math.Max(0, totalIntervalTime - stopwatch.ElapsedMilliseconds); await Task.Delay((int)millisecondsDelay, cancellationToken); } stats.IncrementCompletedDevice(); } catch (OperationCanceledException) when(cancellationToken.IsCancellationRequested) { } catch (Exception ex) { Console.Error.WriteLine(ex); } }