I am hoping this is a pretty simple example of code that I am trying to understand.
I would like to keep starting the process over while waiting for a long running process to complete. The goal is to run all batches at the same time. For example:
while(moreToProcess())
{
var batch = CreateBatch();
await ssisMethod(batch); //takes 30 seconds and would like to start next batch
CreateAndSendReports(batch); //Must wait for ssisMethod to complete
}
I am concerned I don't understand the flow of my code.
as requested:
The important part i think:
public static async Task ssisMehtod(varBatch)
{
using (OleDbConnection conn = new OleDbConnection(cstr))
{
using (OleDbCommand cmd = new OleDbCommand(sql, conn))
{
cmd.Parameters.Add("@p1", OleDbType.Integer).Value = batchID;
cmd.Parameters.Add("@p2", OleDbType.VarWChar, 9).Value = DUNS;
conn.Open();
await cmd.ExecuteNonQueryAsync();
}
}
}
You can save all tasks and run them together after the WhenAll code continues
var tasks = new List<Task>();
while (condition is true)
{
tasks.Add(Task.Run(async () =>
{
var batch = CreateBatch();
await ssisMethod(batch);
CreateAndSendReports(batch);
}));
}
Task.WhenAll(tasks);
To answer you question:
ssisMethod
returns before CreateAndSendReports
To achieve your goal, you can just wrap your method CreateAndSendReports
to async. And create another wrapper method eg ProcessBatch
.
public static async Task ProcessBatch(Batch batch)
{
await ssisMethod(batch).ConfigureAwait(false);
await CreateAndSendReports(batch).ConfigureAwait(false);
}
while(moreToProcess())
{
var batch = CreateBatch();
ProcessBatch(batch).ConfigureAwait(false); //Program will not wait here, it will proceed to create another batch.
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.