Results 1 to 9 of 9

Thread: [RESOLVED] Streamread.close() problems

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    29

    Resolved [RESOLVED] Streamread.close() problems

    sorry i posted on the end of a solved post so I thought i would bring it out as a fresh post.

    I'm trying to monitor a log file. this log updates several time per second at times and im trying to open it read each line to a text box and based on what those lines contain take other actions.

    After reading to the end of the file i close it and open a writer to clear it so im not reading the same text twice.

    The problem comes from the routine firing so quickly back to back that the reader.close() does no release the process in time for it to open again. I need it to be as close to real time as possible

    I get the "file in use by another application" error. Is there something simple im missing? Apologies if this is already solved. I'm still learning.

    It works perfectly the first time the file changes but if there is not a 20 second pause between each call from the monitoring routine i get the error.


    Thanks in advance for any help.
    ****




    Public Sub logupdate(ByVal Source As Object, ByVal e As System.IO.FileSystemEventArgs)
    Dim Reader As TextReader


    Reader = New StreamReader(e.FullPath)
    Dim readText As String

    While Reader.Peek <> -1
    readText = Reader.ReadLine()



    Me.TextBox1.Text = readText
    Select Case Mid(TextBox1.Text, 1, 2)
    Case "hi"
    Me.Label1.Text = "WE got a hit!"
    Case Else
    Me.Label1.Text = "We've got nothing!"
    End Select

    End While
    numLines = lineCount


    Reader.Close()



    Call logclear()
    Reader = Nothing




    End Sub
    ****
    Public Sub logclear()
    Dim sw As StreamWriter
    sw = File.CreateText("filepath.txt")



    sw.Close()
    End Sub

  2. #2
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Streamread.close() problems

    put the code in a Try ... Cach block.
    VB Code:
    1. Try
    2.             'your cod goes her
    3.         Catch
    4.             'do nothing
    5.         End Try

  3. #3
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Streamread.close() problems

    Perhaps this is not your code that is doing it. While the log file is being written to from whatever external source is doing it, you aren't going to be able to access it...

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Streamread.close() problems

    What is calling/invoking the logupdate() sub? It is possible that the events are overlapping, that is while the 1st event is still executing the code, the 2nd event fires...

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    29

    Re: Streamread.close() problems

    Sorry

    I set a file monitor up and it calls the routine on file change.

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Streamread.close() problems

    Quote Originally Posted by sc0ttr0
    Sorry

    I set a file monitor up and it calls the routine on file change.
    That explains it.... In your logupdate sub, you try to read the file, then call clearlog right after that.... clearlog clears the file thus causing file change event to fire ---> logupdate sub runs again, trying to read the file which is still being held by clearlog...

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    29

    Re: Streamread.close() problems

    DOH....I'm such a tard.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    29

    Re: Streamread.close() problems

    I'll try a timer event instead......thanks!

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    29

    Re: [RESOLVED] Streamread.close() problems

    works perfectly inside a timer instead of a filewatch.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width