Let's say i have 2 task (A and B). I can run them in parallel completely, so i can use:
await Task.WhenAll(A,B)
var resA = await A;
var resB = await B;
Now i would like to not wait for B if A (based on some condition) returns false. The result of B does not interest me anymore. Can this be done?
In addition to this answers below:
So just to be sure, a Task starts immediately when creating one. So the following code should work.
var A = Usermanager.FindAsync(email.ToString(), password.ToString());
var B = db.Table.AnyAsync(r=> r.email == email.Tostring()); //simplified action
var user = await A;
if (user != null)
{
var resB = await B;
}
else
{
// A was false, so don't await B
}
And if B generates a long running sql-statement. Would it be better to still use the cancellation token resulting in following code to free up the SQL Servers resources?
var cts = new CancellationTokenSource();
var A = Usermanager.FindAsync(email.ToString(), password.ToString());
var B = db.Table.AnyAsync(r=> r.email == email.Tostring(), cts.Token); //simplified long running sql action
var user = await A;
if (user != null)
{
var resB = await B;
}
else
{
// A was false, cancel query from B
cts.Cancel();
}