I'm using ASP.MVC 5 and EF6. My purpose is to fill database with data from another server and not to block user interface.
Database filling takes a lot of time, about 10-15 minutes. And user can work with system at this time. Now he has very bad freezes for several minutes. So, the fact is, that user load data from base and filling task load data to database in one time.
I've tried to put database filling code to another threads, but it didn't help. When user log in first time, he execute some action, that start database filling. After this action user can do anything else and interface must not be blocked.
When user use the system, he always need to load some data from my database. For example: when user see some page on 1 minute, there is 10000 loaded datas, on 2 minute there is more datas and so on.
public ActionResult SomeAction()
{
new Thread(async () =>
{
Thread.CurrentThread.IsBackground = true;
await ImportTask();
}).Start();
... (some code)
return View();
}
public async Task ImportTask()
{
for (var i = 0; i < 180; i++)
{
using (var context = new ApplicationDBContext())
{
// synchronized data download with some sided library
var data = GetSomeDataFromAnotherServer(i);
foreach (var dataPart in data)
{
var stat = context.Data
.FirstOrDefault(x =>
x.EnumProperty == dataPart.EnumProperty &&
x.LongProperty == dataPart.LongProperty &&
x.DateTimeProperty == dataPart.DateTimeProperty)
?? new DataType()
{
EnumProperty = dataPart.EnumProperty,
LongProperty = dataPart.LongProperty,
DateTimeProperty == dataPart.DateTimeProperty
};
... (some another stat filling)
context.Statistics.AddOrUpdate(stat);
}
await context.SaveChangesAsync();
}
}
}
And there is another question: i have ability to download data with about 10 threads in one time. How i can modify this code to do so?
p.s. code can contain some typos here