Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#-实体框架多线程_C#_Wpf_Multithreading_Entity Framework - Fatal编程技术网

c#-实体框架多线程

c#-实体框架多线程,c#,wpf,multithreading,entity-framework,C#,Wpf,Multithreading,Entity Framework,我想删除一个后台线程上的实体,但我们不能删除另一个线程上创建它的实体,因此如何才能做到这一点并保持ui响应?我打算使用backgroundworker类这里是代码 void deletePeriodWorker_DoWork(object sender, DoWorkEventArgs e) { Thread.Sleep(3000); List<Period> selectedPeriods = e.Argument a

我想删除一个后台线程上的实体,但我们不能删除另一个线程上创建它的实体,因此如何才能做到这一点并保持ui响应?我打算使用backgroundworker类这里是代码

void deletePeriodWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            Thread.Sleep(3000);
            List<Period> selectedPeriods = e.Argument as List<Period>;

            foreach (Period period in selectedPeriods)
            {
                while (period.Transactions.Count > 0)
                {
                    Transaction transaction = period.Transactions[0];
                    this.Dispatcher.Invoke(new Action(() => context.Transactions.Remove(transaction)), 
                        System.Windows.Threading.DispatcherPriority.Normal);
                }

                this.Dispatcher.Invoke(new Action(() => context.Periods.Remove(period)), 
                    System.Windows.Threading.DispatcherPriority.Normal);
            }
            this.Dispatcher.Invoke(new Action(() => context.SaveChanges()),
                    System.Windows.Threading.DispatcherPriority.Normal);
        }
void deletePeriodWorker\u DoWork(对象发送方,DoWorkEventArgs e)
{
睡眠(3000);
列出selectedPeriods=e.参数作为列表;
foreach(选定时段中的时段)
{
而(period.Transactions.Count>0)
{
交易记录=期间。交易记录[0];
this.Dispatcher.Invoke(新操作(()=>context.Transactions.Remove(transaction)),
System.Windows.Threading.DispatcherPriority.Normal);
}
this.Dispatcher.Invoke(新操作(()=>context.Periods.Remove(period)),
System.Windows.Threading.DispatcherPriority.Normal);
}
this.Dispatcher.Invoke(新操作(()=>context.SaveChanges()),
System.Windows.Threading.DispatcherPriority.Normal);
}

不要使用“foreach”删除实体。删除实体时,源已更改,它可能引发异常。用“for”代替。为什么要在新线程中删除它?所有删除操作完成后,UI将更新

试着这样做:

UI(列表):itemsource={Binding LstTest}

背景:

deletePeriodWorker_DoWork() {

List selectedPeriods=e.参数作为列表;
foreach(选定时段中的时段)
{
而(period.Transactions.Count>0)
{
//操作
}
//用户界面更新
this.Dispatcher.Invoke(新操作(()=>lstest.Remove(句点)),
System.Windows.Threading.DispatcherPriority.Normal);
//EF更新
context.Periods.Remove(period);
}
//上下文保存更改

}

看到源是一个普通的
列表
它不能通过删除实体来更改,因此,使用
foreach
非常好,可读性也更高。但是当实体被删除时,如果需要一段时间,ui会保持响应吗?是的,你是对的。我没有看到代码是
context.Periods.Remove(XX)
。我们无法在
foreach
中向集合添加/删除项目。您好,假设我要删除1000个事务,这可能需要2/3秒,那么接下来会发生什么?莫蒂,请尝试另一种方法。不要在删除实体和UI更新之间创建关系。UI的itemsource与列表绑定,请在新线程中添加/删除其项。不要删除新线程中的Entite。
        List<Period> selectedPeriods = e.Argument as List<Period>;

        foreach (Period period in selectedPeriods)
        {
            while (period.Transactions.Count > 0)
            {
                //operation
            }

            //ui updating
            this.Dispatcher.Invoke(new Action(() => LstTest.Remove(period)), 
                System.Windows.Threading.DispatcherPriority.Normal);

            //EF updating
            context.Periods.Remove(period);

        }

        //context savechanges