I need help understanding how Task
work in c#
. I want to call ExecuteClause
once for every clause in clauses
. I've figured out that I should create a list of tasks and then
await Task.WhenAll(taskList);
Finally I can get the result of each task. This is what I have so far:
public async Task Execute(string[] clauses)
{
var taskList = new List<Task<in>>();
// Here I want to call ExecuteClause for each string in clauses
// and wait for the result.
}
private async Task<int> ExecuteClause(string clause)
{
var connectionString = ConfigurationManager.ConnectionStrings["default"].ConnectionString;
var connection = new SqlConnection(connectionString);
await connection.OpenAsync();
var r = await new SqlCommand(clause, connection).ExecuteReaderAsync();
while (await r.ReadAsync())
{
...
}
r.Close();
connection.Close();
// Just an example returning an int
return 1;
}