首先,当我启动调用以下函数DrawDTSRanksChart()的线程时,它给了我一个错误“调用线程必须是STA,因为许多UI组件需要这个”
将以下行添加到线程调用
之后UpdateThread.SetApartmentState(ApartmentState.STA)
“调用线程无法访问此对象,因为其他线程拥有它”
注释行抛出两个异常
Public Update Thread As System.Threading.Thread
Private Sub DrawDTSRanksChart()
Dim DTSRankingDiagram As New XYDiagram2D
Dim series1 As New AreaFullStackedSeries2D
ChartControl1.Diagram = DTSRankingDiagram 'this line throws the exption
DTSRankingDiagram.Series.Add(series1)
series1.Points.Add(New SeriesPoint("Ranked 1.", DTSr1counter))
series1.Points.Add(New SeriesPoint("Ranked 2.", DTSr2counter))
series1.Points.Add(New SeriesPoint("Ranked 3.", DTSr3counter))
series1.Points.Add(New SeriesPoint("Ranked >3.", DTSrm3counter))
End Sub
Private Sub save_Clicked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles NormalUpdate.ItemClick
UpdateThread = New System.Threading.Thread(AddressOf update)
UpdateThread.SetApartmentState(ApartmentState.STA)
UpdateThread.Start()
End Sub
我只需要一个后台工作人员,所以我的UI不会挂起
答案 0 :(得分:0)
解决方案
Dim MyDispatcher = Windows.Threading.Dispatcher.CurrentDispatcher
Private Delegate Sub UpdateUIDelegate()
并将该函数调用为
MyDispatcher.BeginInvoke(DispatcherPriority.Background, New UpdateUIDelegate(AddressOf DrawDTSRanksChart))