问题描述
应用程序背景:由WPF和vb.net设计的桌面应用程序由sql server express 2008备份,适用于整个工作组中的多个用户.
我们正在尝试使用调度程序计时器将数据定期刷新到数据网格中.问题在于,即使在通过分派器通过委托函数调用设置了数据网格的数据上下文之后,UI仍然冻结,并且我们无法更新进度条.我想知道这种方法是否完全错误?还是在连接线程/进程时缺少某些东西?
示例:(在后面的代码中)
Application Background: Desktop app designed in WPF and vb.net backed-up by sql server express edition 2008 for multiple users across the workgroup.
We are trying to refresh the data periodically into the datagrids using dispatchertimers. The problem is that even after setting the datacontexts of the datagrids from a delegate function call through a dispatcher the UI still freezes and we are not able to update the progressbars. I would like to know if this approach is totally wrong? or we are missing something when it comes to wiring the threads/processes?
example: (In code behind)
Private WithEvents timer1 As DispatcherTimer
Dim WithEvents dOP As DispatcherOperation
Delegate Sub updatemainUIDelegate()
Private Sub timer1_tick(ByVal sender As Object, ByVal e As EventArgs) Handles timer1.Tick
'...
dOP = Me.RefreshDataBtn.Dispatcher.BeginInvoke(DispatcherPriority.Background, (New updatemainUIDelegate(AddressOf updateMainUI)))
'...
End Sub
Public Sub updateMainUI()
Debug.Print("Updating UI")
Me.RecentWorkOrdersDataGrid.DataContext = PMS_DistributableDataSetRecentWorkOrdersTableAdapter.Fill(PMSDS.RecentWorkOrders)
Me.RecentProposalsDataGrid.DataContext = PMS_DistributableDataSetRecentProposalsTableAdapter.Fill(PMSDS.RecentProposals)
'(several datagrids and tableadapters to refresh)
'...
End Sub
(在XAML中)
(In XAML)
<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml /presentation" xmlns:my1="clr-namespace:PMS_WPF">
<Window.Resources>
<myPMS:PMS_DistributableDataSet x:Key="PMS_DistributableDataSet" />
<CollectionViewSource x:Key="WorkOrdersViewSource" Source="{Binding Path=WorkOrders, Source={StaticResource PMS_DistributableDataSet}}" />
<CollectionViewSource x:Key="ProjectsViewSource" Source="{Binding Path=Projects, Source={StaticResource PMS_DistributableDataSet}}" />
<!--... -->
<DataGrid ItemsSource="{Binding Source={StaticResource RecentWorkOrdersViewSource}}" Name="RecentWorkOrdersDataGrid" ... />
如何防止UI冻结和异步刷新数据网格?
(注意:
1.数据可以由用户手动/以选定的时间间隔(即15/30/45分钟)刷新.
2.请不要解释这种刷新的缺点,即同时进行编辑/更新等.
3.我们正在使用在.xsd中自动创建的表和tableadapters以及映射.用这种组合在任何地方都找不到足够的解决方案,因此是一个问题)
What can be done to prevent the UI freeze and refresh of the datagrids asynchronously?
(Note:
1. The data can be refreshed manually/at chosen time interval by user i.e. 15/30/45 mins.
2. Please do not explain on disadvantages of such refreshing i.e. simultaneous edits/updates etc...it has been taken care of.
3. We are using the tables and tableadapters and mapping created in the .xsd automatically. Could not find enough solutions anywhere with this combination hence the question)
Thanks in advance!
推荐答案
这篇关于WPF Datagrid:在没有UI冻结的情况下使用Dispatcher刷新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!