实体框架SaveChangesAsync阻止我的UI? [英] Entity Framework SaveChangesAsync blocking my UI?

查看:118
本文介绍了实体框架SaveChangesAsync阻止我的UI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新新p新新新新旗新新新新旗新新旗新新旗新新新旗新新旗200新新新新旗新新旗200新新新旗200新新新新旗新新旗新新款: (int i = 0; i< 10000; i ++)
{
context.Table1.Add(new Table1(){Name =jon+ i}

);
}

await context.SaveChangesAsync();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);

}
}

在此代码中,context.SaveChangesAsync阻止我的UI直到保存数据库中的所有记录。
为什么会发生这种情况?
我如何解决?



更新:

  private async void button9_Click(object sender,EventArgs e)
{
try
{
MyDbContext context = new MyDbContext(); X-454545454545 X- 20045 X-454545 X- 20045 X-454545 X- 20045 X-454545 X- 20045 X- 20045 X- 20045 X-4545 X- 20045 X- 20045 X- );
}
MessageBox.Show(Start);
等待Task.Delay(20000);
MessageBox.Show(End);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);

}
}

为什么在上述代码UI中阻塞直到延迟结束?
但是在以前的代码UI被阻止?

解决方案

它是等待行导致问题。



尝试这样:

}

await context.SaveChangesAsync();
});
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);

}
}

这将使所有的代码运行异步地,不应该阻止UI了。您可以尝试调试 for循环 MyDbContext 行,看看为什么他们花费的时间比他们想要的要多。 / p>

private async void button4_Click(object sender, EventArgs e)
        {
            try
            {
                MyDbContext context = new MyDbContext();

                for (int i = 0; i < 10000; i++)
                {
                    context.Table1.Add(new Table1() { Name = "jon" + i});
                }

                await context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }

in this code, context.SaveChangesAsync blocking my UI until Saving all records in database. Why is this happening? How do i fix it?

Update:

private async void button9_Click(object sender, EventArgs e)
    {
        try
        {
            MyDbContext context = new MyDbContext();

            for (int i = 0; i < 10000; i++)
            {
                context.Table1.Add(new Table1() { Name = "jon" + i });
            }
            MessageBox.Show("Start");
            await Task.Delay(20000);
            MessageBox.Show("End");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);

        }
    }

Why in the above code UI not blocking until delay ends? But in the previous code UI is blocked?

解决方案

It is the code above the await line which is causing the problems.

Try this:

private async void button4_Click(object sender, EventArgs e)
        {
            try
            {
                await Task.Run(() =>
                {
                    MyDbContext context = new MyDbContext();

                    for (int i = 0; i < 10000; i++)
                    {
                        context.Table1.Add(new Table1() { Name = "jon" + i });
                    }

                    await context.SaveChangesAsync();
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }

This will make all the code run asynchronously and should not block the UI anymore. You can try debugging the for loop and MyDbContext lines and see why they're taking more time than they should.

这篇关于实体框架SaveChangesAsync阻止我的UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆