|
-
Jan 17th, 2007, 05:40 PM
#1
Thread Starter
Junior Member
[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
-
Jan 17th, 2007, 06:05 PM
#2
Re: Streamread.close() problems
put the code in a Try ... Cach block.
VB Code:
Try
'your cod goes her
Catch
'do nothing
End Try
-
Jan 17th, 2007, 06:20 PM
#3
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...
-
Jan 17th, 2007, 07:12 PM
#4
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...
-
Jan 19th, 2007, 10:47 AM
#5
Thread Starter
Junior Member
Re: Streamread.close() problems
Sorry
I set a file monitor up and it calls the routine on file change.
-
Jan 19th, 2007, 11:15 AM
#6
Re: Streamread.close() problems
 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...
-
Jan 19th, 2007, 12:04 PM
#7
Thread Starter
Junior Member
Re: Streamread.close() problems
-
Jan 19th, 2007, 12:11 PM
#8
Thread Starter
Junior Member
Re: Streamread.close() problems
I'll try a timer event instead......thanks!
-
Jan 19th, 2007, 02:34 PM
#9
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|