-1

起初,当我启动调用以下函数 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 不会挂起

4

1 回答 1

0

解决方案

Dim MyDispatcher = Windows.Threading.Dispatcher.CurrentDispatcher
Private Delegate Sub UpdateUIDelegate()

并将函数称为

 MyDispatcher.BeginInvoke(DispatcherPriority.Background, New UpdateUIDelegate(AddressOf DrawDTSRanksChart))
于 2012-08-18T20:06:51.990 回答