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.
The assumption here is that I don’t want to assume which tasks run faster – some might complete instantly, some might take quite some time to complete if they need to query a database or file, for example.
This implementation has and will continue to change frequently, and I don’t want to be constantly updating this code to try and optimize the check order (these types of checks will be repeated in hundreds of classes).
Is there a way to accomplish this? For example, something like Task.TrueForAll(…) that cancels the other tasks once any task returns false?
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!
5