• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Visual basic text file nightmare

Joined
Dec 22, 2007
Messages
184 (0.03/day)
Location
Central Nebraska
System Name CORSAIR
Processor AMD Phenom 1090T x6 @3.2
Motherboard Gigabyte Ga-78LMT-S2P
Cooling High efficiency dust cooling
Memory 8GB GSkill DDR3 1333
Video Card(s) Sapphire Radeon HD3870 512MB GDDR4 PCI-e toxic
Storage Seagate SV35.3 ST3250310SV 250GB SATAII(Windows 7 Pro x64), 320GB Samsung SATAII(Storage and SuSE)
Display(s) Dell 20" LCD, Dell 17" LCD
Power Supply Antec Cool Blue 650W Modular
Software Windows 7 Professional SP1, Visual Studio 2010 Ultimate
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
 

temp02

New Member
Joined
Mar 18, 2009
Messages
493 (0.09/day)
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.
 

W1zzard

Administrator
Staff member
Joined
May 14, 2004
Messages
26,957 (3.71/day)
Processor Ryzen 7 5700X
Memory 48 GB
Video Card(s) RTX 4080
Storage 2x HDD RAID 1, 3x M.2 NVMe
Display(s) 30" 2560x1600 + 19" 1280x1024
Software Windows 10 64-bit
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
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.65/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
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.
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.21/day)
Location
Cheeseland (Wisconsin, USA)
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
 
Joined
Nov 30, 2008
Messages
555 (0.10/day)
Location
Birmingham, England...
Processor Intel Core 2 Quad Q6600 @ 2.8GHz
Motherboard Gigabyte X48T-DQ6
Cooling Zalman 9500 LED CPU Cooler
Memory 2x 2GB Corsair DDR3 XMS3 DHX - 1600MH/PC3-12800
Video Card(s) Gigabyte HD4870 1GB
Storage 2x Seagate 320GB Barracuda (RAID 0) 3x 1TB Samsung F3, 140GB WD Maxtor (10,000rpm)
Display(s) 2x 20" LG Flatron L204WS
Power Supply Powercool 850W
Software Windows 7 Ultimate x64
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

Been a while since I monitored this topic, I don't think I ever got around to saying... THANK YOU KREIJ for helping me graduate university!!!
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.65/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
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.
 
Joined
Dec 13, 2007
Messages
2,758 (0.46/day)
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...
 
Joined
Dec 1, 2007
Messages
528 (0.09/day)
Location
Karachi, Pakistan
System Name mAkS
Processor Intel Core 2 Duo E0 E8400 @3.0Ghz (Acheived 4.4Ghz On Stock Cooling)
Motherboard Asus P5K-V (Dead :[ )
Cooling CPU + Front + Rear + Side
Memory XMS2 DHX TWIN2X2048-6400C5DHX/800 BUS
Video Card(s) Inno3D 9600 GT Overclocked
Storage Seagate Sata II 250 GB ++ 500 GB
Display(s) Hp 2009f 20-inch Wide-screen
Case Legend
Audio Device(s) Build-In 8 CHannel (Dell 5.1")
Power Supply CoolerMaster 460W eXtreme Power Plus
Software Many
Benchmark Scores E8400 hardware rank 94th on hwbot in wPrime :D
Application.DoEvents()

This might solve the problem, Which version of VB are you using?
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.21/day)
Location
Cheeseland (Wisconsin, USA)
Been a while since I monitored this topic, I don't think I ever got around to saying... THANK YOU KREIJ for helping me graduate university!!!

My pleasure, Akumos, always happy to help any way I am able.
Congrats on your graduation (I can't believe you didn't invite me to your graduation party. :shadedshu :laugh:) and I think I will add "Helped people graduate" to my resume. lol
 
Joined
Dec 22, 2007
Messages
184 (0.03/day)
Location
Central Nebraska
System Name CORSAIR
Processor AMD Phenom 1090T x6 @3.2
Motherboard Gigabyte Ga-78LMT-S2P
Cooling High efficiency dust cooling
Memory 8GB GSkill DDR3 1333
Video Card(s) Sapphire Radeon HD3870 512MB GDDR4 PCI-e toxic
Storage Seagate SV35.3 ST3250310SV 250GB SATAII(Windows 7 Pro x64), 320GB Samsung SATAII(Storage and SuSE)
Display(s) Dell 20" LCD, Dell 17" LCD
Power Supply Antec Cool Blue 650W Modular
Software Windows 7 Professional SP1, Visual Studio 2010 Ultimate
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:
Joined
Dec 13, 2007
Messages
2,758 (0.46/day)
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

now to have to it read it every 5000ms (5sec)

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
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.21/day)
Location
Cheeseland (Wisconsin, USA)
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. :toast:
 
Joined
Dec 13, 2007
Messages
2,758 (0.46/day)
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. :toast:

ah yea i didn't catch that :p
 
Joined
Dec 22, 2007
Messages
184 (0.03/day)
Location
Central Nebraska
System Name CORSAIR
Processor AMD Phenom 1090T x6 @3.2
Motherboard Gigabyte Ga-78LMT-S2P
Cooling High efficiency dust cooling
Memory 8GB GSkill DDR3 1333
Video Card(s) Sapphire Radeon HD3870 512MB GDDR4 PCI-e toxic
Storage Seagate SV35.3 ST3250310SV 250GB SATAII(Windows 7 Pro x64), 320GB Samsung SATAII(Storage and SuSE)
Display(s) Dell 20" LCD, Dell 17" LCD
Power Supply Antec Cool Blue 650W Modular
Software Windows 7 Professional SP1, Visual Studio 2010 Ultimate
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
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.65/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
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.
 
Joined
Dec 22, 2007
Messages
184 (0.03/day)
Location
Central Nebraska
System Name CORSAIR
Processor AMD Phenom 1090T x6 @3.2
Motherboard Gigabyte Ga-78LMT-S2P
Cooling High efficiency dust cooling
Memory 8GB GSkill DDR3 1333
Video Card(s) Sapphire Radeon HD3870 512MB GDDR4 PCI-e toxic
Storage Seagate SV35.3 ST3250310SV 250GB SATAII(Windows 7 Pro x64), 320GB Samsung SATAII(Storage and SuSE)
Display(s) Dell 20" LCD, Dell 17" LCD
Power Supply Antec Cool Blue 650W Modular
Software Windows 7 Professional SP1, Visual Studio 2010 Ultimate
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?
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.65/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
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?
 
Joined
Dec 22, 2007
Messages
184 (0.03/day)
Location
Central Nebraska
System Name CORSAIR
Processor AMD Phenom 1090T x6 @3.2
Motherboard Gigabyte Ga-78LMT-S2P
Cooling High efficiency dust cooling
Memory 8GB GSkill DDR3 1333
Video Card(s) Sapphire Radeon HD3870 512MB GDDR4 PCI-e toxic
Storage Seagate SV35.3 ST3250310SV 250GB SATAII(Windows 7 Pro x64), 320GB Samsung SATAII(Storage and SuSE)
Display(s) Dell 20" LCD, Dell 17" LCD
Power Supply Antec Cool Blue 650W Modular
Software Windows 7 Professional SP1, Visual Studio 2010 Ultimate
there will be two separate txt files monitored in two separate text or combo boxes side by side.
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.21/day)
Location
Cheeseland (Wisconsin, USA)
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.
 
Joined
Dec 22, 2007
Messages
184 (0.03/day)
Location
Central Nebraska
System Name CORSAIR
Processor AMD Phenom 1090T x6 @3.2
Motherboard Gigabyte Ga-78LMT-S2P
Cooling High efficiency dust cooling
Memory 8GB GSkill DDR3 1333
Video Card(s) Sapphire Radeon HD3870 512MB GDDR4 PCI-e toxic
Storage Seagate SV35.3 ST3250310SV 250GB SATAII(Windows 7 Pro x64), 320GB Samsung SATAII(Storage and SuSE)
Display(s) Dell 20" LCD, Dell 17" LCD
Power Supply Antec Cool Blue 650W Modular
Software Windows 7 Professional SP1, Visual Studio 2010 Ultimate
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
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.65/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
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.
If file.Length < lastoffset Then
' 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.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.65/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
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.
 
Top