WPF BeginInvoke和EntityFramework [英] WPF BeginInvoke and EntityFramework

查看:119
本文介绍了WPF BeginInvoke和EntityFramework的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我通过数据库进行搜索.搜索在单独的线程中进行.找到实体后,我必须将其和一些相关数据显示到WPF UI中.

我使用EntityFramework.搜索过程的主要思想是:

Hi
I have a search through database. Search works in separate thread. When entity is found I have to show it and some of related data into WPF UI.

I use EntityFramework. Main idea of search process is:

foreach (var item in _currentEntitySet)
{     
    Items.Add(item);
    OnItemFound(item);
}



其中_currentEntitySet是一个ObjectQuery

但是我遇到了一些问题.触发OnItemFound时,我尝试使用BeginInvoke()在UI中显示找到的项目和一些相关对象.



Where _currentEntitySet is an ObjectQuery

But I have met some problems. When OnItemFound is fired, I try to use BeginInvoke() to display found item and some related object in UI.

private void OnCatalogueItemFound(CatalogueItem item)
{
    Application.Current.Dispatcher.BeginInvoke(new Action<object>((param) =>
    {
        var model = new CatalogueResultItemViewModel(param as CatalogueItem);
        TitlesResultViewModel.Add(model);
    }), System.Windows.Threading.DispatcherPriority.Background, item);
}



问题在于项目的导航属性为NULL

当我使用Invoke()而不是BeginInvoke()时,一切正常.由于其他一些原因,我必须完全使用BeginInvoke().

有谁知道在我的情况下如何使用BeginInvoke()?谢谢:)



The problem is that navigation properties of item are NULL

When I use Invoke() instead of BeginInvoke() then things works fine. I have to use exactly BeginInvoke() because of some other reasons.

Does anyone knows how can I use BeginInvoke() in my situation? Thanks :)

推荐答案

请参阅Mark的评论和我对问题的评论.

您只需要了解InvokeBeginInvoke操作之间的区别,并自己推断结论,因为只有您拥有完整的代码.从一种方法更改为另一种方法可能会更改调用顺序.

顺便说一句,您可以通过使用类System.Diagnostics.EventLog http: //en.wikipedia.org/wiki/种族条件 [^ ]).

从UI线程之外的任何线程调用InvokeBeginInvoke所使用的委托都不会在调用线程上被调用.而是通过将其添加到UI线程的事件队列中来调度的. UI检索进行调用所需的委托和参数,并在UI线程上调用它.如果使用BeginInvoke,则该方法将在委托和调用参数排队后立即返回,但是Invoke阻止调用,直到在UI线程上实际调用了委托为止.这种不同的行为可能导致某些操作的顺序不同.

您可以分析代码和/或使用EventLog找出会发生什么.

另请参阅我过去的解决方案:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [
Please see the comment by Mark and my comment to the question.

You just need to understand the difference between Invoke and BeginInvoke operations and infer the conclusion by yourself because only you have the whole code. A change from one method to another may change the order of calls.

By the way, you can observe the order of execution it by logging some information form different place of your code using the class System.Diagnostics.EventLog http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^]. If you use the Debugger, you can introduce time delay which can change the picture (race condition, see http://en.wikipedia.org/wiki/Race_condition[^]).

The delegate used in the call to Invoke or BeginInvoke from any thread other then the thread of you UI is never called on a calling thread. Instead, it is dispatched by adding it to an event queue of the UI thread. The UI retrieve the delegate and parameters required to make a call and calls it on the UI thread. If you use BeginInvoke, the method returns right after the delegate and call parameters are queued, but Invoke is blocking the called until the delegate is actually called on the UI thread. This different behaviors can cause different order of some operations.

You can analyze your code and/or use EventLog to figure out what happens.

See also my past solutions:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

—SA


这篇关于WPF BeginInvoke和EntityFramework的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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