【发布时间】:2015-07-16 00:04:51
【问题描述】:
我的 WPF 应用程序中有一个数据网格,当应用程序启动时,我从数据库中获取部署记录并将它们加载到绑定到数据网格的 ObservableCollection 中。
通过计时器,我使用 BackgroundWorker 从数据库中获取任何新记录并将它们放入新的 ObservableCollection。
通过 RunWorkerCompleted,我尝试使用新 ObservableCollection 中的项目更新数据网格。
但是,我收到“System.NotSupportedException”。
附加信息:这种类型的 CollectionView 不支持从不同于 Dispatcher 线程的线程更改其 SourceCollection。
我以为我可以从 RunWorkerCompleted 方法访问 UI 控件,但似乎发生了一些我不明白的事情。
我的代码如下:
private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
{
//Init the deployment collection
deployments = DataAccess.GetDeployments();
dgDeployments.ItemsSource = deployments;
//Setup the background worker
bw = new BackgroundWorker();
bw.DoWork += bw_DoWork;
bw.RunWorkerCompleted += bw_RunWorkerCompleted;
bw.WorkerReportsProgress = true;
bw.WorkerSupportsCancellation = true;
//Setup timer
System.Threading.Timer t = new System.Threading.Timer(new System.Threading.TimerCallback(TimerProc));
t.Change(10000, 0);
}
private void TimerProc(object state)
{
//MessageBox.Show("Timer fired!");
//Get the latest currentTime from the items in the grid
DateTime? latestTime = DataAccess.GetDeployments().ToList()[0].CurrentTime;
bw.RunWorkerAsync(latestTime);
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
//Get new records that have changed since that time
e.Result = (ObservableCollection<Deployment>)DataAccess.GetDeployments((DateTime)e.Argument);
}
void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if(e.Error != null)
{
MessageBox.Show(e.Error.Message);
}
else if (e.Cancelled)
{
MessageBox.Show("The operation was cancelled");
}
else
{
ObservableCollection<Deployment> newDeployments = (ObservableCollection<Deployment>)e.Result;
foreach (Deployment d in newDeployments)
{
//Remove this new/changed deployment from the collection bound to the datagrid
int index = deployments.IndexOf(deployments.Where(x => x.UniqueID == d.UniqueID).FirstOrDefault());
if (index > -1)
{
deployments.RemoveAt(index);
}
//Now add the new deployments
deployments.Add(d);
deployments.Move(deployments.IndexOf(d), 0);
}
}
}
【问题讨论】:
标签: c# wpf timer datagrid backgroundworker