I have created a thread which have to do some xml parsing in its class. The class Inherits from System.Windows.Threading.DispatcherObject
while its parsing i wanna get the dispatcher to add the items to a ObservableCollection(Of T)
i will add my code here and describe the problem in the comments
Private Delegate Sub NewsDelegate(ByVal news As NewsItemClass)
Public Sub ReadAllNews()
Dim th As New System.Threading.Thread(AddressOf StartReadNews)
th.IsBackground = True
th.Start()
End Sub
Private Sub StartReadNews()
Dim xDoc As XDocument = XDocument.Load("myXML.xml")
For Each newsElement As XElement In xDoc.<news>.<item>
Dim id As Integer = CInt(newsElement.@id)
Dim newsItem As New NewsItemClass
With newsItem
.ID = CInt(newsElement.@id)
.Subject = newsElement.<subject>.Value
.NewsDate = newsElement.<date>.Value
.Author = newsElement.<author>.Value
.Content = newsElement.<content>.Value
End With
'Try
'Dim del As New NewsDelegate(AddressOf AddNews)
'del.Invoke(newsItem)
'Catch ex As Exception 'throws: Von diesem CollectionView-Typ werden keine Änderungen der "SourceCollection" unterstützt, wenn diese nicht von einem Dispatcher-Thread aus erfolgen. (whining for a dispatcher)
'End Try
Try
If Me.Dispatcher.CheckAccess() Then
Me.AddNews(newsItem)
Else 'comes in here
Me.Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Normal, New NewsDelegate(AddressOf AddNews), newsItem) 'nothing happens
End If
Catch ex As Exception 'never thrown
Debug.WriteLine(ex.Message)
End Try
Next
xDoc = Nothing
End Sub
Friend Sub AddNews(ByVal newsItem As NewsItemClass) 'never get called
SyncLock Me.News 'tried without synclock but no effect either
Me.News.Add(newsItem)
Debug.WriteLine(Me.News.Count)
End SyncLock
End Sub
I hope someone can tell me whats wrong on my code plz
ow, one more thing...im using .NET 4.0