1

This is the comment from my first question showing the code. Process1 calls a sub named ArchDtlCopyFile1 which scrolls through the values in a list view, copying the files named in the items to a different location. It then calls ArchDtlCheckCopy1 to scroll through the list view again to ensure that the copy was done. It then decides if the source file should be deleted and do it if required. Finally it inserts a row in an Access table documenting the change made.

        Private Sub Process1()
    If ReturnCode = 0 Then
        ArchDtlCopyFile1()
    Else
        ' MessageBox.Show("Error code coming in is: " & CStr(ReturnCode))
    End If

    If ReturnCode = 0 Then
        ArchDtlCheckCopy1()
    Else
        '  MessageBox.Show("Error code for check copy is: " & CStr(ReturnCode))
    End If
End Sub
Private Sub ArchDtlCopyFile1()

    intLVIndex = 0

    ' Copy the file from the source computer onto the NAS
    Do While intLVIndex < intMaxFileIndex
        Try

            ' Select the row from the LVFiles ListView, then move the first column (0) into strSourceFilePath and the last
            ' column (3) into strDestFilePath. Execute the CopyFile method to copy the file.
            LVFiles.Items(intLVIndex).Selected = True
            strSourceFilePath = LVFiles.SelectedItems(intLVIndex).SubItems(0).Text
            strDestFilePath = LVFiles.SelectedItems(intLVIndex).SubItems(3).Text
            My.Computer.FileSystem.CopyFile(strSourceFilePath, strDestFilePath, overwrite:=False)

        Catch ex As Exception

            ' Even if there's an error with one file, we should continue trying to process the rest of the files
            Continue Do

        End Try

        intLVIndex += 1

    Loop

End Sub

Private Sub ArchDtlCheckCopy1()

    intLVIndex = 0
    intLVError = 0

    '    ' Check each file was copied onto the NAS
    Do While intLVIndex < intMaxFileIndex


        ' Select the row from the LVFiles ListView, then move the last column (3) into strDestFilePath.
        ' Use the FileExists method to ensure the file was created on the NAS. If it was, call the
        ' ADetDelete Sub to delete the source file from the user's computer.

        LVFiles.Items(intLVIndex).Selected = True
        strSourceFilePath = LVFiles.SelectedItems(intLVIndex).SubItems(0).Text
        strDestFilePath = LVFiles.SelectedItems(intLVIndex).SubItems(3).Text
        strSourceFile = LVFiles.SelectedItems(intLVIndex).SubItems(1).Text
        Try

            If My.Computer.FileSystem.FileExists(strDestFilePath) Then
                ' Archive file was created so go ahead and delete the source file
                'If strSourceFile = myCheckFile Then
                '    strDestFile = LVFiles.SelectedItems(intLVIndex).SubItems(3).Text
                'End If
                If RBArchive.Checked = True Then
                    ArchDtlDeleteFile(strSourceFilePath)
                End If
                PrepareDtlVariables()
                ADtlAddRow()

            Else
                MessageBox.Show("File not found. " & strDestFilePath)
            End If

        Catch ex As Exception

            ErrorCode = "ARC6"
            MessageCode = "Error while checking file copy"
            ReturnCode = 8

        End Try

        intLVIndex += 1
    Loop

End Sub
Ross from Brooklin
  • 293
  • 1
  • 5
  • 18
  • Ok. What is your question? – Caius Jard Jul 15 '20 at 21:47
  • 2
    Duplicate of [How do I use VB Application.DoEvent?](https://stackoverflow.com/questions/62922012/how-do-i-use-vb-application-doevent) – Andrew Morton Jul 15 '20 at 21:59
  • 2
    Instead of `My.Computer.FileSystem.CopyFile`, you can use [Stream.ReadAsync](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.readasync) and [Stream.WriteAsync](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.writeasync), so you can change your methods using the `async/await` pattern and the problem is solved (adding some exception/cancellation handling). With `Application.DoEvents`, you're looking for trouble (when dealing with File operations, real troubles - The *go find another job* type of trouble). – Jimi Jul 15 '20 at 22:07
  • lol I'm doing this for free for a friend so I don't think he can fire me. I'll wait and see. – Ross from Brooklin Jul 16 '20 at 13:13

0 Answers0