I’ve got a list of tasks that check conditions, returning true or false values.
public override async Task<TradeEntry?> TryGetNewTradeEntryAsync(RequestNewTradeEntry request)
{
if (await condition.FalseAsync<IsPeriodEnd>
(new(request, this.Settings.PeriodGap, this.productCode)))
{
return null;
}
if (await condition.FalseAsync<EventActiveWithAvailableEntries>
(new(request, this.productCode, this)))
{
return null;
}
if (await condition.FalseAsync<IsAfterNewsEvent>
(new(request, 1, this.Period1m, this.productCode)))
{
return null;
}
if (await condition.TrueAsync<AfterNewsClosedOverExtremeSwingPointBeforeNews>
(new(request, this.Settings.SwingPointLookback, request.Bias.Opposite(), this.Period1m, this.productCode)))
{
// find a gap...
}
else if (await condition.TrueAsync<HasNoUnclosedOverSwingPointBeforeNews>
(new(request, this.Settings.SwingPointLookback, request.Bias.Opposite(), this.Period1m, this.productCode)))
{
// look for new swing points closed over...
}
}
In many cases, I don’t necessarily need them to run sequentially.
I might just want to return null as soon as a single task returns false, for example, and skip checks on the other conditions.
Is there a way to accomplish this? For example, a Task.WhenAll(…) – but instead of WhenAll, WhenAny?
Returning early is important since I’m running this approximately 35 million times in a loop, and don’t want to waste time checking conditions that don’t need to be checked.
Or, does this look like an approach that’s likely to create more problems down the road (e.g. threading issues)?
Thanks!