neodammer Posted August 4, 2005 Posted August 4, 2005 (edited) Ive figured out a way to read a text file while in use and read each line when two files are not the same (the one being used by another process and duplicate one i made for vb access.) The problem lies in this text file being somewhat large >1MB and you know when my timer keeps checking for changes and loading the text file all over again it MAJORLY hogs up cycles. My question is: Is there a way to keep it so the text file in question is loaded into the ram or something? Maybe buffering if i understand what that means correctly. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'check to see if file exists if so delete and recopy Dim checkfile As String checkfile = Dir$("C:\access2.log") If checkfile = "C:\access2.log" Then Kill("C:\access2.log") End If System.IO.File.Copy("C:\access.log", "C:\access2.log") 'copy file because its in use 'end check Timer1.Enabled = False End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Dim fileDetails_new As System.IO.FileInfo = New System.IO.FileInfo("C:\access2.log") Dim fileDetails_reg As System.IO.FileInfo = New System.IO.FileInfo("C:\access.log") If fileDetails_new.Length.ToString() <> fileDetails_reg.Length.ToString() Then Dim sr As StreamReader = New StreamReader("C:\access2.log") Dim line As String Do 'looping reading each line at a time line = sr.ReadLine() If TextBox1.Text = "" Then 'make sure you dont have a blank line at start TextBox1.Text = Mid(line, 1, 13) Else TextBox1.Text = TextBox1.Text & vbCrLf & Mid(line, 1, 13) End If Loop Until line = Nothing 'loop until the end Me.Show() 'set the caret and redraw to scroll to bottom TextBox1.SelectionStart = TextBox1.Text.Length TextBox1.ScrollToCaret() sr.Close() 'close the streamreader End If Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing Kill("C:\access2.log") End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Timer1.Enabled = True End Sub As you can see the access file is constantly being changed so the program is completely copying and reloading the text file into the textbox which again is chewing cycles. anyway to stop it from such a huge lag and just write Edited August 4, 2005 by neodammer Quote Enzin Research and Development
Administrators PlausiblyDamp Posted August 4, 2005 Administrators Posted August 4, 2005 Just out of interest what is generating the file in the first place? Also instead of just looping in a timer you could use a FileSystemWatcher component from the toolbar to notify you on changes to the file. Also rather than reading the file line by line you could do a sr.ReadToEnd() to read the entire file into memory in one hit andthen process it. Possibly even skipping to the last read line and only appending new data to the textbox rather than reloading the entire thing. Although it won't solve your problem you might want to replace some of the VB6 code (Dir etc) with the .Net equivalent Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'check to see if file exists if so delete and recopy If File.Exists("C:\access2.log") Then File.Delete("C:\access2.log") End If System.IO.File.Copy("C:\access.log", "C:\access2.log") 'copy file because its in use 'end check Timer1.Enabled = False End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Dim fileDetails_new As FileInfo = New FileInfo("C:\access2.log") Dim fileDetails_reg As FileInfo = New FileInfo("C:\access.log") If fileDetails_new.Length.ToString() <> fileDetails_reg.Length.ToString() Then Dim sr As StreamReader = New StreamReader("C:\access2.log") Dim line As String Do 'looping reading each line at a time line = sr.ReadLine() If TextBox1.Text = "" Then 'make sure you dont have a blank line at start TextBox1.Text = line.Substring(1, 13) Else TextBox1.Text = TextBox1.Text & vbCrLf & line.Substring(1, 13) End If Loop Until line = Nothing 'loop until the end Me.Show() 'set the caret and redraw to scroll to bottom TextBox1.SelectionStart = TextBox1.Text.Length TextBox1.ScrollToCaret() sr.Close() 'close the streamreader End If End Sub Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing File.Delete("C:\access2.log") End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Timer1.Enabled = True End Sub Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
neodammer Posted August 4, 2005 Author Posted August 4, 2005 (edited) Could you give a sample code on how I would append a .txt file with readtoend followed by appending? I know how to use readtoend just to import a file etc. not sure exactly how to tell it to append the difference between the files. It is Apache's access file im trying to create a simple monitor for :D Hmmm what about if I find the # of lines in the file and then compare it and then if different just goto the last line of the old file and write whatever is new to a stored variable and display that? seems then it wouldnt have to to load the entire .txt file everytime. Edited August 5, 2005 by neodammer Quote Enzin Research and Development
neodammer Posted August 5, 2005 Author Posted August 5, 2005 (edited) Here is the project in zip file. You can see what im trying to do better this way and hopefully help me come up with a solution lol i can get it to work just it keeps reloading a huge text file sometimes and all i need it to is update the new data. eeek sorry bout the binaries forgot to remove them thanks ;)APACHEREADER.zip Edited August 5, 2005 by neodammer Quote Enzin Research and Development
neodammer Posted August 9, 2005 Author Posted August 9, 2005 err just a thought and a bump as well (im still without a solution to this rofl) but this error is it a memory leak as well? Seems possible but not sure i havent tested it yet. Quote Enzin Research and Development
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.