I have a window with a telerik gridview in it and i update this gridview every 1 minute. i have a refresh method and every 1 minute i call it.
i cant use BackgroundWorker because my CollectionViewSource is in the UI thread and backgroundworker cant use it. my question is how can i call this refresh method from another thread?
somewhere i see this sample code:
Application.Current.Dispatcher.BeginInvoke(new Action(() => this.Refresh()));
is this a true way that i use the above code in Refresh_Executed?
please help me.
this is my Refresh method:
public ObservableCollection<RequestView> AllRequestsData { get; set; }
private void Refresh()
{
using (ArchiveEntities db = new ArchiveEntities())
{
var data = db.RequestSyncs.Where(x => x.UserId == null);
if (data.Any())
{
string IdList = String.Join(",", data.Where(x => x.IsNew).Select(x => x.RequestId));
if (!String.IsNullOrWhiteSpace(IdList))
{
foreach (var item in db.RequestViews.Where("it.id in {" + IdList + "}"))
{
this.AllRequestsData.Add(item);
}
}
foreach (var item in data.Where(x => x.IsDeleted))
{
RequestView rv = this.AllRequestsData.Where(x => x.Id == item.RequestId).SingleOrDefault();
if (rv != null)
{
this.AllRequestsData.Remove(rv);
}
}
foreach (var item in data)
{
db.RequestSyncs.DeleteObject(item);
}
db.SaveChanges();
}
}
}
thanks
May be in common it’s true way, but it depends on content of the
Refreshmethod, could you provide the content of it?EDIT
Usually I load data in view model using async theads and don’t really need to update RadGridView. But when, for instance, I need to collapse all groups e.t.c I use radGridView.Dispatcher.BeginInvoke(…).
UPDATE
There is no need to update
RadGridView. You are updating it’sItemsSourceproperty. I would not reccomend to useDispatcherin such situation and I would use Task Parallel Library to recieve plain data from database and updateItemsSourcewith the synchronization with the current UI thread:Note, that you should upgrade this method to implement your needs (you could put you database methods in first task and may be return
Tuplefrom two colelctions) and this is just idea.