I need to update my database with about 100K records over a slow network.
I thought it might be a good idea to run them async and not have to wait for the response before I run the next one. I've got this far:
private async void button2_Click(object sender, EventArgs e)
{
var customerIDs = File.ReadAllLines(@"c:\temp\customerids.txt").ToList();
foreach (var customerID in customerIDs)
{
var result = await DoSomethingWithTheCustomerAsync(customerID);
}
MessageBox.Show("Done");
}
private static async Task<bool> DoSomethingWithTheCustomerAsync(string strCustomerid)
{
var customerid = int.Parse(strCustomerid);
using (var ctx = new MyEntities())
{
var customer = await ctx.Customers.FirstOrDefaultAsync(c => c.CustomerID == customerid);
customer.CustomerNotes += " updated async";
await ctx.SaveChangesAsync();
return true;
}
}
I'd really don't want to wait for the result in the foreach-loop and then continue. I'd like to wait for all the tasks after the loop has run. I tried making a list with tasks and do Task.WaitAll(tasklist) but then the database wasn't updated.
And if this can be done, is it a good idea?