Thread problem, backgroundworker?


New member
Sep 11, 2019
Programming Experience
Word 2016, Visual Studio 2019
Hello everybody,
I hope, that you can help me with my problem.
My issue:
I have three folders with word-documents (.docx), each more than 100 documents.
Each folder contents documents with a different theme.
I want to select documents by different properties, e.g. words of the content.
With the start of the word-application I start a backgroundworker, that collects the important data of each document and stores them in a Dictionary(Of String, String).
All datasets are stored in an array of three ObservableCollections, one for each path/group of documents: 'Public Shared Dateilisten(2) As ObservableCollection(Of Dictionary(Of String, String))'
The array is part of a class 'Worddatei', that represents the properties of the word-files.
With myWorker.reportprogress I deliver each set of data ('myDataset') to the sub 'ReportProgress': 'Private Shared Sub ReportProgress(ByVal sender As System.Object, ByVal e As ProgressChangedEventArgs) Handles MyWorker.ProgressChanged'.
This sub adds the dataset to the correct ObservableCollection in the array 'Dateilisten()'.
Then it raises the public event 'Public Shared Event DateilisteAktualisiert(ProcessPercentage As Integer)'.
When I show the Window, eventually not all Word-Documents are loaded.
So the window shall show the analysed filenames and add each new filename to the corresponding listbox, when the 'ReportProgress'-event of the backgroundworker is raised.
This event is handled by a Sub of the class 'MainWindow.xaml.vb', which is a code-behind-class of a WPF-Window: 'AddHandler DateilisteAktualisiert, AddressOf PBarFortschritt_ChangeValue'.
In that moment when the event is raised, it is not possible to get or set values to the controls of the window, as I get the following error message: System.InvalidOperationException; The calling thread cannot get the object, because it is owned by another thread.
Please tell me, how I can solve the problem or identify the bug.

This is the code of the backgroundworker:

Private Shared Sub Do_work(ByVal sender As System.Object, ByVal e As DoWorkEventArgs) Handles MyWorker.DoWork
   Thread.CurrentThread.Priority = ThreadPriority.Lowest
   Dim Index = 0
   Dim myDataset As Dictionary(Of String, String) ' = New Dictionary(Of String, String)
   Dim myPaths(2) As String
   myPaths(0) = My.Resources.Path0
   myPaths(1) = My.Resources.Path1
   myPaths(2) = My.Resources.Path2
   For Each Path In myPaths
      For Each element In Directory.GetFiles(Path)
         Index += 1
         If IO.Path.GetExtension(element) = ".docx" OrElse
            IO.Path.GetExtension(element) = ".docm" OrElse
            IO.Path.GetExtension(element) = ".doc" Then
            myDataset = Worddataset(element)
            MyWorker.ReportProgress(Index, myDataset)
         End If
         If MyWorker.CancellationPending Then
            e.Cancel = True
            Exit Sub
         End If
End Sub
Private Shared Function Worddataset(Fullname As String) As Dictionary(Of String, String)
   Worddataset = New Dictionary(Of String, String)
      Using Dokument = WordprocessingDocument.Open(Fullname, False)
         Worddataset.Add(PROP_FULLNAME, Fullname)
      End Using
   Catch ex As Exception
   End Try
End Function
And this is the 'ProgressChanged'-code of the class 'Worddatei' that represents the specific custom properties of the selected word-file, that contains all Datasets of the documents in a shared ObservableCollection-Array and that holds the backgroundworker-Subs:
(Status and myStatus represents the property that shows, which ObservableCollection of the array is the correct one.

 Private Shared Sub ReportProgress(ByVal sender As System.Object, ByVal e As ProgressChangedEventArgs) Handles MyWorker.ProgressChanged
     Dim myDict = CType(e.UserState, Dictionary(Of String, String))
    Dim myStatus = Status(myDict)
    RaiseEvent DateilisteAktualisiert(e.ProgressPercentage)
 End Sub
This is the code in the MainWindows-Sub, that is added to the Handler of 'DateilisteAktualisiert'
PBarFortschritt is the Processbar, that shall show the progress of the Backgroundworker.
But in this moment no control-property of the MainWindow is available.
It doesn't matter, if the Sub is attached to DateilisteAktualisiert or to the changed-Event of the ObservableCollection.

Private Sub PBarFortschritt_ChangeValue(Anzahl As Integer)
        PBarFortschritt.Foreground = COLOR_GREEN.Brush
        PBarFortschritt.Value = Anzahl * 100 / _Total
    End Sub
Thank you very much for your help
Top Bottom