This question answers how we can asynchronously wait for a task to finish with a timeout.
I have written a similar async method as shown below, which checks if the current value is greater that some defined threshold value.
public async Task<bool> FindThreshold(CancellationTokenSource cts)
{
await Task.Run(()=> {
if (this.currentValue >= this.ThresholdValue) {
this.ThresholdFound = true;
} else {
this.ThresholdFound = false;
}
});
if (ThresholdFound)
return true;
else
return false;
}
Based on the similar solution given I am calling the function as shown below,
var found = await FindThresholdAsyncAsync(cts);
if (await Task.WhenAny(task, Task.Delay(
TimeSpan.FromMilliseconds(Convert.ToDouble(timeout)*1000),
cts.Token) == found)
{
await found;
}
The error message says that Operator "==" cannot be applied to operands of the type 'Task' and 'bool'. I do understand that Task.Delay returns a task but I couldn't figure out why it is working in case of the solution mentioned here. Clearly, I am missing something so any insights would be really helpful.