Question

I'm really struggling with Cross Threading and could do with a pointer or 2!

The below is run from a menu option from a right click menu on a treeview and basically extracts certain files from a directory (GetCISFiles) copies them to a new folder then zips them up. The respective node should then be cleared from the treeview. I keep getting the Cross Thread Operation not Valid error. I've debugged through and the error appears right before the final Message Box appears. As in the code i've tried a delegate sub for clearing the node (which is where i think the problem is) however its still an issue.

Dim x As Integer
    For x = 0 To UBound(AllDetails)
        Dim startpath As String = tempMail & tvProgress.SelectedNode.FullPath

        Dim fileout As String = outMail & AllDetails(x).uCode & "-" & AllDetails(x).uOps & "-" & Date.Parse(AllDetails(x).pDate).ToString("dd-MM-yyyy")
        lstPlanned.BeginUpdate()

        Try
            Call GetCISFiles(startpath)

            Dim zip As String = inMail & AllDetails(x).uFile & ".zip"
            Dim txt As String = inMail & AllDetails(x).uFile & ".txt"
            Dim pdf As String = inMail & AllDetails(x).uFile & ".pdf"
            Dim fldr As String = inMail & AllDetails(x).uFile & "\"

            ZipFile.CreateFromDirectory(fileout & "\", fileout & ".zip", CompressionLevel.Optimal, False)
            Directory.Delete(fileout & "\", True)
            File.Delete(zip)
            File.Delete(txt)
            File.Delete(pdf)
            Directory.Delete(fldr, True)

            My.Computer.FileSystem.DeleteDirectory(tempMail & tvProgress.SelectedNode.FullPath, FileIO.UIOption.AllDialogs, FileIO.RecycleOption.SendToRecycleBin)

            Dim del As ClearNode
            del = New ClearNode(AddressOf ClearNodea)
            del.Invoke()

            Dim sRoot As String = tempMail

            Dim sPath As String = tempMail

            DeleteEmptyFolders(sPath, sRoot)

            MsgBox("Job sent to CIS")

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    Next
    lstPlanned.EndUpdate()
End Sub

Delegate Sub

Public Delegate Sub ClearNode()
Private Sub ClearNodea()
    tvProgress.SelectedNode.Remove()
End Sub

Looks like it comes back to the following code

 watchfolder = New System.IO.FileSystemWatcher()

    watchfolder.Path = My.Settings.Out

    watchfolder.Filter = "*.zip"

    AddHandler watchfolder.Created, AddressOf CompleteArray
    AddHandler watchfolder.Deleted, AddressOf CompleteArray


    watchfolder.EnableRaisingEvents = True

Where would i begin to look at invoking here?

No correct solution

OTHER TIPS

The UI elements cannot be accessed from a background thread. To make this work you need to use Invoke to execute the Remove method on the UI thread.

Private Sub ClearNodea()
  Dim act As Action = Sub() tvProgress.SelectedNode.Remove()
  tvProgress.Invoke(act)
End Sub

For older versions of .Net, you can do:

Private Delegate Sub ClearNodeDelegate()
Private Sub ClearNode()
    If tvProgress.InvokeRequired Then
        tvProgress.Invoke(New ClearNodeDelegate(AddressOf ClearNode))
    Else
        tvProgress.SelectedNode.Remove()
    End If
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top