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);
}
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);
}
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 🙂
I think that this may be because you are generating your object with a lambda function.
BeginInvoke is called asynchroimously and possibly by the time it is actioned the object locatedis no longer in scope.
By using Invoke you force the call to be performed synchronously, when the object is still in scope.