More concise? I'd say you're near the limit already; the only line I could realistically eliminate is rpt--;
(by hoisting it up: while (rpt-- > -1) { ... }
). All other ways I can imagine of implementing the same logic lead to less terse code.
This should run your group of asynchronous methods in parallel using LINQ to generate a list of tasks:
var tasks = Enumerable
.Range(0, Settings.rpt)
.Select(async _ => {
await ShowPhraseHeading();
await ShowPhraseDetail();
await ShowSleep();
});
await Task.WhenAll(tasks);
To be clear, ShowPhraseHeading()
, ShowPhraseDetail()
, and ShowSleep()
should happen sequentially within each block while the blocks themselves will run in parallel. If you want/need everything to run sequentially then maybe go with this? (I personally like your existing code style better):
Enumerable
.Range(0, Settings.rpt)
.Select(async _ => {
await ShowPhraseHeading();
await ShowPhraseDetail();
await ShowSleep();
})
.Select(task => task.GetAwaiter().GetResult()) // wait for each block to complete
.ToList(); // a dumb way to force enumeration
Note: I chose not to iterate backwards because it didn't seem to matter in your particular case; .Reverse()
might be appropriate if you need to change direction at any point.
Assuming that one chooses to run things in parallel then it might be a good idea to check out the posts here in order to put a limit on the maximum degree of parallelism.