I am trying to use asynchronous programming with async
and await
to calculate some values and save in a database periodically using ASP.NET Core 2.x. The method shown below is not executing from the thread pool.
public async void CalculateCIndex(int CIndexId)
{
List<string> UserIds = _context.ApplicationUsers
.Where(m => m.EmailConfirmed == true)
.Select(m => m.Id).ToList();
for (int i = 0; i < UserIds.Count; i += 10)
{
var temp = UserIds.Skip(i).Take(10);
foreach (var Id in temp)
{
await Task.Run(async () =>
{
await CIndexCal(Id, CIndexId);
});
}
await Task.Delay(6000);
}
}
public async Task CIndexCal(string Id, int CIndexId)
{
_context.CIndexMember.Add(new CIndexMember
{
NCIndex = (decimal)((AindexAfterRar + Power) / CI.TotalCindex)
});
_context.SaveChanges();
}