-1

at first when i started the thread which calls the following function DrawDTSRanksChart() it gave me an error "The calling thread must be STA, because many UI components require this"

after adding the follwing line to the thread call

UpdateThread.SetApartmentState(ApartmentState.STA)

"The calling thread cannot access this object because a different thread owns it"

the commented line throws both exception

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

i just need a background worker so my UI doesnt hang

user1570048
  • 880
  • 6
  • 35
  • 69
  • possible duplicate of [How to deal with cross-thread access exceptions?](http://stackoverflow.com/questions/11923865/how-to-deal-with-cross-thread-access-exceptions) – H.B. Aug 18 '12 at 19:00

1 Answers1

0

Solution

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

and call the function as

 MyDispatcher.BeginInvoke(DispatcherPriority.Background, New UpdateUIDelegate(AddressOf DrawDTSRanksChart))
user1570048
  • 880
  • 6
  • 35
  • 69