DoEvents problem

hog

Senior Contributor
Joined
Mar 17, 2003
Messages
984
Location
UK
I have code running detailed below that uses a FileSystemWatcher to check for the creation of a file which then call the GenerateP2Textfile function.

This function uses a loop with a DoEvents call in it and stays in the loop until the file in question has content.

The loop exits OK as the rest of the code after the loop runs OK as does the app.

However when everything else is all done and dusted the code when I'm stepping through it returns to the DoEvents loop??

Have I missed something??


Visual Basic:
    Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed

        Dim fwFileWatcher As FileSystemWatcher = DirectCast(sender, FileSystemWatcher)

        ' call sub to extract PIs from p1.txt and write them to p2.txt

        If Not GenerateP2Textfile(fwFileWatcher) Then

            Return

        End If

        ' this will only get checked if the p2.txt was created successfully

        Me.chkP1Detected.Checked = True

End Sub


Visual Basic:
  Private Function GenerateP2Textfile(ByVal fwFileWatcher As FileSystemWatcher) As Boolean

        Dim strFileName As String

        ' get the full path of the p1.txt file

        strFileName = fwFileWatcher.Path & "\" & fwFileWatcher.Filter

        ' open a fileinfo object against p1.txt

        Dim fFileInfo As FileInfo = New FileInfo(strFileName)

        ' stay in this loop until circe-mi has written data to p1.txt
        Do

            Application.DoEvents()

        Loop Until fFileInfo.Length > 0

        Dim srFile1 As StreamReader
        Dim srFile2 As StreamWriter
        Dim strDelimiter As String = ControlChars.Tab
        Dim chrDelimiter As Char() = strDelimiter.ToCharArray()
        Dim strWords As String
        Dim strSplit As String() = Nothing

        Try

            ' open a stream reader to read p1.txt

            srFile1 = New StreamReader(strFileName)

            ' open a stream writer to write to p2.txt

            srFile2 = New StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\p2.txt", True)

            Dim intX As Integer

            ' read in one line at a time

            strWords = srFile1.ReadLine

            Do While strWords <> Nothing

                ' decipher the delimiters used

                For intX = 1 To 9

                    strSplit = strWords.Split(chrDelimiter, intX)

                Next intX

                ' write the first field out to p2.txt

                srFile2.WriteLine(strSplit(0))

                ' read in the next line

                strWords = srFile1.ReadLine

            Loop

            Return True

        Catch objException As Exception

            ShowError("Location:   Class frmCIRCEImports" & ControlChars.CrLf & ControlChars.CrLf & _
                      "Procedure: GenerateP2Textfile(ByVal fwFileWatcher As FileSystemWatcher)" & ControlChars.CrLf & ControlChars.CrLf & "Error Text: " & _
                      objException.Message)

            Return False

        Finally

            srFile2.Flush()
            srFile2.Close()

        End Try

End Function
 
Mmm this appears to solve the problem....

Visual Basic:
 Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed

        ' create file watcher to check for when the data has been exported from CIRCE-MI

        Me.FileSystemWatcher6.Path() = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
        Me.FileSystemWatcher6.Filter = "p1.txt"
        Me.FileSystemWatcher6.NotifyFilter = NotifyFilters.LastWrite

        ' create the event handlers for the file watchers and pass the address of the code that should run

        AddHandler FileSystemWatcher6.Created, AddressOf FileSystemWatcher6_Changed

        ' start watching

        Me.FileSystemWatcher6.EnableRaisingEvents = True

End Sub

   Private Sub FileSystemWatcher6_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher6.Changed

        Dim fwFileWatcher As FileSystemWatcher = DirectCast(sender, FileSystemWatcher)

        ' call sub to extract PIs from p1.txt and write them to p2.txt

        If Not GenerateP2Textfile(fwFileWatcher) Then

            Return

        End If

        ' this will only get checked if the p2.txt was created successfully

        Me.chkP1Detected.Checked = True

End Sub

the generatep2text file function remains the same without the loop.

Therefore running it now does not result in 100% cpu usage:)
 
Back
Top