![]() |
|
|
#1 |
![]() Join Date: Dec 2007
Location: Central Nebraska
Posts: 170 (0.09/day)
Thanks: 85
Thanked 7 Times in 6 Posts
|
Visual basic text file nightmare
Hello, I am writing a program that needs to be able to read from a text file and display its output into a multiline text box. I have this part done. my problem is, I need this to update in real time as the text file im reading from is a log that keeps track of uploading of files etc. the method i had had in mind to update the text box in real time was to loop the reading of the text file and add a thread.sleep(5000) line in the loop, giving 5000 milliseconds before it refreshes- this method causes the program to freeze. any other ideas on how to do this? Thanks in advance
-sean |
|
|
|
|
|
#2 |
![]() Join Date: Mar 2009
Posts: 490 (0.32/day)
Thanks: 0
Thanked 171 Times in 158 Posts
|
It has been while since I've messed with Visual Basic but I'm pretty sure there is a visual component called Timer (or something equivalent), add one to the form, double click on it and add your "loop" code, now without the loop, to it, then set the timer interval property to 5000 and your set.
|
|
|
|
| The Following User Says Thank You to temp02 For This Useful Post: |
|
|
#3 |
|
Benevolent Dictator
Join Date: May 2004
Location: Stuttgart, Germany
Posts: 13,766 (4.18/day)
Thanks: 184
Thanked 10,213 Times in 3,159 Posts
|
read up on FindFirstChangeNotification and related functions, for the correct way to do this (in a second thread)
what is probably easier for you is using settimer to periodically get a timer called |
|
|
|
| The Following User Says Thank You to W1zzard For This Useful Post: |
|
|
#4 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,574 (6.29/day)
Thanks: 1,752
Thanked 2,596 Times in 1,960 Posts
|
If it were me, I'd make a separate class with the reader in it then have it raise an event whenever new lines were detected (each event raised would contain the line). That way, the GUI only updates when it needs to.
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
| The Following User Says Thank You to FordGT90Concept For This Useful Post: |
|
|
#5 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,113 (5.28/day)
Thanks: 591
Thanked 5,492 Times in 2,936 Posts
|
You didn't mention what application is doing the writing to the text file, but you will probably want to implement error checking in there when reading the file.
If the app doing the writing has an exclusive lock on the file that prevents any other app from accessing it for either reading or writing, your app will throw access denied exceptions when trying to open it for a read. Disclaimer : It's Monday
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
|
|
#6 | |
![]() Join Date: Nov 2008
Location: Birmingham, England...
Posts: 472 (0.29/day)
Thanks: 72
Thanked 27 Times in 22 Posts
|
Quote:
__________________
![]() |
|
|
|
|
| The Following User Says Thank You to Akumos For This Useful Post: |
|
|
#7 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,574 (6.29/day)
Thanks: 1,752
Thanked 2,596 Times in 1,960 Posts
|
FYI, you can work around file locks by having CMD do a copy. CMD copy ignores file locks. Read the copied file, then delete the copy when done.
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
| The Following User Says Thank You to FordGT90Concept For This Useful Post: |
|
|
#8 |
![]() Join Date: Dec 2007
Location: East TN
Posts: 2,392 (1.20/day)
Thanks: 408
Thanked 439 Times in 342 Posts
|
Hope i'm not to late... But like said by others
Use a timer. set the internal to 5000.... if it still cause freezes. I would try "backgroundworker" to grab the text file, and then have timer to read it... |
|
|
|
| The Following User Says Thank You to CrackerJack For This Useful Post: |
|
|
#9 |
![]() Join Date: Dec 2007
Location: Karachi, Pakistan
Posts: 506 (0.25/day)
Thanks: 42
Thanked 13 Times in 10 Posts
|
Application.DoEvents()
This might solve the problem, Which version of VB are you using? |
|
|
|
| The Following User Says Thank You to smartali89 For This Useful Post: |
|
|
#10 | |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,113 (5.28/day)
Thanks: 591
Thanked 5,492 Times in 2,936 Posts
|
Quote:
Congrats on your graduation (I can't believe you didn't invite me to your graduation party. ) and I think I will add "Helped people graduate" to my resume. lol
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
|
| The Following 3 Users Say Thank You to Kreij For This Useful Post: |
|
|
#11 |
![]() Join Date: Dec 2007
Location: Central Nebraska
Posts: 170 (0.09/day)
Thanks: 85
Thanked 7 Times in 6 Posts
|
Sorry for the long response, but I am just now getting back to this abandoned part in the project -
I have resorted to just creating this in its own seperate app till I get this working before implementing it in the project. SO... I have some code for you guys. This is the best thing I have got so far... IF you use this in a consoel application and change the line that outputs this to the textbox to "console.writeline(line)" it works flawlessly. But as a windows forms application... nooot so much. try it and see. ----------------------------------------- Imports System.IO Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Using reader As New StreamReader(New FileStream("C:\logtest\test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) 'start at the end of the file Dim lastMaxOffset As Long = reader.BaseStream.Length Do System.Threading.Thread.Sleep(100) 'if the file size has not changed, idle If reader.BaseStream.Length = lastMaxOffset Then Continue Do End If 'seek to the last max offset reader.BaseStream.Seek(lastMaxOffset, SeekOrigin.Begin) 'read out of the file until the EOF Dim line As String = "" line = reader.ReadLine() Do While line IsNot Nothing TextBox1.AppendText(line) line = reader.ReadLine() Loop 'update the last max offset lastMaxOffset = reader.BaseStream.Position Loop End Using End Sub End Class Edit: P.s. Using Visual Studio 2010 Ultimate Last edited by shuggans; Aug 14, 2011 at 06:15 AM. Reason: added visual studio version |
|
|
|
|
|
#12 |
![]() Join Date: Dec 2007
Location: East TN
Posts: 2,392 (1.20/day)
Thanks: 408
Thanked 439 Times in 342 Posts
|
If your only reading the first line of the text file use something like this... I don't see the point of running a loop if only the first line is been read, But of coarse this will read the line no matter what.
Code:
Using reader As New StreamReader("C:\logtest\test.txt")
'Reading only the first line in test.txt
Dim line As String = ""
line = reader.ReadLine()
TextBox1.AppendText(line)
End Using
Code:
Imports System.IO
Public Class Main
Private Sub Main_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ReaderTimer.Start()
End Sub
Private Sub ReaderTimer_Tick(sender As System.Object, e As System.EventArgs) Handles ReaderTimer.Tick
ReaderTimer.Interval = 5000
Using reader As New StreamReader("C:\logtest\test.txt")
'Reading only the first line in test.txt
Dim line As String = ""
line = reader.ReadLine()
TextBox1.AppendText(line)
End Using
End Sub
End Class
|
|
|
|
| The Following User Says Thank You to CrackerJack For This Useful Post: |
|
|
#13 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,113 (5.28/day)
Thanks: 591
Thanked 5,492 Times in 2,936 Posts
|
He's not reading the first line. He's reading any new lines that have been appended to the end of the file since the last read.
Something you may want to consider, shuggans, is append the new lines to the top of the TextBox so that the newest entry in the file is always at the top. You could then truncate the contents of the textbox if it gets longer than you want. Just a suggestion.
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
| The Following User Says Thank You to Kreij For This Useful Post: |
|
|
#14 | |
![]() Join Date: Dec 2007
Location: East TN
Posts: 2,392 (1.20/day)
Thanks: 408
Thanked 439 Times in 342 Posts
|
Quote:
|
|
|
|
|
| The Following User Says Thank You to CrackerJack For This Useful Post: |
|
|
#15 |
![]() Join Date: Dec 2007
Location: Central Nebraska
Posts: 170 (0.09/day)
Thanks: 85
Thanked 7 Times in 6 Posts
|
I need to have the whole log available to view-
The log isnt updated all that often, and the point of it in the program is so you never have to hunt the log down and open it in notepad. would it be better to use something other than a textbox to do this, such as a combobox? also, crackerjack, I haven't used timers since my IRC script kiddie days. Certainly never in VB before, how do I properly define the timer used in the code you posted, and I could try just modifying it to readtoend |
|
|
|
|
|
#16 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,574 (6.29/day)
Thanks: 1,752
Thanked 2,596 Times in 1,960 Posts
|
You never want to read-to-end because it is inefficient in every regard (unless it is a very, very small file). What you'll want to do is keep record of the offset of your last read and whenever the file length changes, jump to the last offset then read the lines until the end of file (rince and repeat).
I would probably use a ListBox--one item per line--perhaps bound to a stack to always place the most recent item at the top.
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
| The Following User Says Thank You to FordGT90Concept For This Useful Post: |
|
|
#17 |
![]() Join Date: Dec 2007
Location: Central Nebraska
Posts: 170 (0.09/day)
Thanks: 85
Thanked 7 Times in 6 Posts
|
and kreij, If you try the code I had as a console app, it works great. In a windows form, it causes the app to become unresponsive. and idea why? this has to be possible to do...
Is there a way to embed a console into a vb.net form? |
|
|
|
|
|
#18 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,574 (6.29/day)
Thanks: 1,752
Thanked 2,596 Times in 1,960 Posts
|
If the application ever becomes unresponsive, it is because something is tying up the main thread that needs to be moved to a worker thread.
No, you can simulate a console in a Windows Form but they are different (CUI vs GUI) on an executable level. How many files are you trying to monitor? Just one?
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
| The Following User Says Thank You to FordGT90Concept For This Useful Post: |
|
|
#19 |
![]() Join Date: Dec 2007
Location: Central Nebraska
Posts: 170 (0.09/day)
Thanks: 85
Thanked 7 Times in 6 Posts
|
there will be two separate txt files monitored in two separate text or combo boxes side by side.
|
|
|
|
|
|
#20 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,113 (5.28/day)
Thanks: 591
Thanked 5,492 Times in 2,936 Posts
|
Since your loop is in the main UI thread it's causing the UI to be unresponsive.
Put that in a worker thread as Ford stated. I see some issues with maintaining an offset on repeated runs of the app, as if the logfile gets deleted or truncated, the offset will be wrong.
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
| The Following User Says Thank You to Kreij For This Useful Post: |
|
|
#21 |
![]() Join Date: Dec 2007
Location: Central Nebraska
Posts: 170 (0.09/day)
Thanks: 85
Thanked 7 Times in 6 Posts
|
This is my firs whack at multi threading, what did I do wrong, form still locks up:
Imports System.IO Imports System.Threading Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click readlog() End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Public Sub readlog() Dim Thread_UploadLogReader As New System.Threading.Thread(AddressOf readlog) Thread_UploadLogReader.Start() Using reader As New StreamReader(New FileStream("C:\logtest\test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) 'start at the end of the file Dim lastMaxOffset As Long = reader.BaseStream.Length Do System.Threading.Thread.Sleep(100) 'if the file size has not changed, idle If reader.BaseStream.Length = lastMaxOffset Then Continue Do End If 'seek to the last max offset reader.BaseStream.Seek(lastMaxOffset, SeekOrigin.Begin) 'read out of the file until the EOF Dim line As String = "" line = reader.ReadLine() Do While line IsNot Nothing TextBox1.AppendText(line) line = reader.ReadLine(line) Loop 'update the last max offset lastMaxOffset = reader.BaseStream.Position Loop End Using End Sub End Class |
|
|
|
|
|
#22 |
![]() Join Date: Dec 2007
Location: East TN
Posts: 2,392 (1.20/day)
Thanks: 408
Thanked 439 Times in 342 Posts
|
Please wrap your code
![]() I believe i see one issue, updating VS atm... so gimme a few till i can post back |
|
|
|
| The Following User Says Thank You to CrackerJack For This Useful Post: |
|
|
#23 | |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,574 (6.29/day)
Thanks: 1,752
Thanked 2,596 Times in 1,960 Posts
|
Quote:
' Start over End If Edit: I'm trying to think if I already coded a generic line parser but I don't think I did. They were always specific to a format (broke down every line into parts) so nothing I have would work out of the box. It really wouldn't be hard to make one though.
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
|
| The Following User Says Thank You to FordGT90Concept For This Useful Post: |
|
|
#24 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,113 (5.28/day)
Thanks: 591
Thanked 5,492 Times in 2,936 Posts
|
What if the logfile was deleted and has grown longer than the offset already?
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
| The Following User Says Thank You to Kreij For This Useful Post: |
|
|
#25 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,574 (6.29/day)
Thanks: 1,752
Thanked 2,596 Times in 1,960 Posts
|
That would result in an "oops." That issue is negated by more frequently checking the size (like 100ms instead of 5000ms).
You can also go by date-modified too but, in this situation, I'd perfer length so if the application that's writing the log just touches the file (opens a write stream and closes it), it doesn't invoke an update.
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
| The Following User Says Thank You to FordGT90Concept For This Useful Post: |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Visual Basic Help | shuggans | Programming & Webmastering | 3 | Feb 6, 2011 12:27 PM |
| Visual basic help | shuggans | Programming & Webmastering | 10 | Feb 4, 2011 02:11 PM |
| Visual Basic | shuggans | Programming & Webmastering | 4 | Dec 10, 2010 10:35 PM |
| Visual Basic Tutorials | Wozzer | Programming & Webmastering | 9 | Nov 30, 2008 10:06 AM |
| Visual Basic HELP | dcf-joe | Programming & Webmastering | 6 | Mar 5, 2008 01:36 PM |