Ästhetische Frage wirklich.Verkettung von C# async/warten auf Tasks, sie neu erstellen, wie sie fertig sind
diesen Code (Polling unvermeidlich) Gegeben:
protected override async Task ExecuteAsync(CancellationToken ct)
{
// move the loop here somehow?
await Task.WhenAll(
Task.Run(async() => await this.PollA(ct), ct),
Task.Run(async() => await this.PollB(ct), ct),
Task.Run(async() => await this.PollC(ct), ct))
.ConfigureAwait(false);
}
die Methoden zur Zeit wie folgt aussehen Polling, hat jeder eine andere Verzögerung.
private async Task Poll(CancellationToken ct)
{
while (!ct.IsCancellationRequested)
{
await Task.Delay(Math.Max(1000, CONFIGA), ct);
this._logger.StartAction("poll A status");
this._logger.StopAction("poll A status");
}
}
Gibt es eine Möglichkeit, eine Fortsetzung zu strukturieren, dass die Schleife in jedem der Poll
Methoden
private async Task Poll(CancellationToken ct)
{
await Task.Delay(Math.Max(1000, CONFIGA), ct);
this._logger.StartAction("poll A status");
this._logger.StopAction("poll A status");
}
Dies könnte nicht einmal seine das richtige Muster entfernt, aber es scheint besser als drei Endlosschleifen haben.
Warum nicht einfach 'erwarten Task.WhenAll (this.PollA (ct), this.PollB (ct), this.PollC (ct)) '? Aber sicherlich können Sie einfach 'while (! Ct.IsCancellationRequested) auf Task.WhenAny warten (this.PollA (ct), this.PollB (ct), this.PollC (ct))' wenn das das gewünschte Verhalten wäre. –