techPowerUp! Forums

Go Back   techPowerUp! Forums > Software > Programming & Webmastering

Reply
 
Thread Tools
Old Mar 28, 2011, 06:28 PM   #1
shuggans
75 Posts
 
Join Date: Dec 2007
Location: Central Nebraska
Posts: 170 (0.09/day)
Thanks: 85
Thanked 7 Times in 6 Posts

System Specs

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
shuggans is offline  
Reply With Quote
Old Mar 28, 2011, 06:44 PM   #2
temp02
200 Posts
 
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.
temp02 is offline  
Reply With Quote
The Following User Says Thank You to temp02 For This Useful Post:
Old Mar 28, 2011, 06:53 PM   #3
W1zzard
Benevolent Dictator
 
W1zzard's Avatar
 
Join Date: May 2004
Location: Stuttgart, Germany
Posts: 13,766 (4.18/day)
Thanks: 184
Thanked 10,213 Times in 3,159 Posts
Send a message via ICQ to W1zzard Send a message via AIM to W1zzard Send a message via MSN to W1zzard

System Specs

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
W1zzard is online now  
Reply With Quote
The Following User Says Thank You to W1zzard For This Useful Post:
Old Mar 28, 2011, 09:01 PM   #4
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,574 (6.29/day)
Thanks: 1,752
Thanked 2,596 Times in 1,960 Posts

System Specs

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(); }
FordGT90Concept is offline  
Crunching for Team TPU
Reply With Quote
The Following User Says Thank You to FordGT90Concept For This Useful Post:
Old Mar 28, 2011, 09:39 PM   #5
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,113 (5.28/day)
Thanks: 591
Thanked 5,492 Times in 2,936 Posts

System Specs

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
Kreij is online now  
Reply With Quote
The Following 2 Users Say Thank You to Kreij For This Useful Post:
Old Mar 29, 2011, 09:46 PM   #6
Akumos
200 Posts
 
Akumos's Avatar
 
Join Date: Nov 2008
Location: Birmingham, England...
Posts: 472 (0.29/day)
Thanks: 72
Thanked 27 Times in 22 Posts

System Specs

Quote:
Originally Posted by Kreij View Post
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!!!
__________________
Akumos is offline  
Reply With Quote
The Following User Says Thank You to Akumos For This Useful Post:
Old Mar 29, 2011, 09:51 PM   #7
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,574 (6.29/day)
Thanks: 1,752
Thanked 2,596 Times in 1,960 Posts

System Specs

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(); }
FordGT90Concept is offline  
Crunching for Team TPU
Reply With Quote
The Following User Says Thank You to FordGT90Concept For This Useful Post:
Old Apr 1, 2011, 01:30 AM   #8
CrackerJack
2000 Posts
 
CrackerJack's Avatar
 
Join Date: Dec 2007
Location: East TN
Posts: 2,392 (1.20/day)
Thanks: 408
Thanked 439 Times in 342 Posts

System Specs

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...
CrackerJack is offline  
Reply With Quote
The Following User Says Thank You to CrackerJack For This Useful Post:
Old Apr 1, 2011, 12:57 PM   #9
smartali89
500 Posts
 
smartali89's Avatar
 
Join Date: Dec 2007
Location: Karachi, Pakistan
Posts: 506 (0.25/day)
Thanks: 42
Thanked 13 Times in 10 Posts
Send a message via MSN to smartali89 Send a message via Yahoo to smartali89

System Specs

Application.DoEvents()

This might solve the problem, Which version of VB are you using?
__________________
Funny and Humorous Stuff

smartali89 is offline  
Reply With Quote
The Following User Says Thank You to smartali89 For This Useful Post:
Old Apr 2, 2011, 12:00 AM   #10
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,113 (5.28/day)
Thanks: 591
Thanked 5,492 Times in 2,936 Posts

System Specs

Quote:
Originally Posted by Akumos View Post
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. ) 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
Kreij is online now  
Reply With Quote
The Following 3 Users Say Thank You to Kreij For This Useful Post:
Old Aug 14, 2011, 06:03 AM   #11
shuggans
75 Posts
 
Join Date: Dec 2007
Location: Central Nebraska
Posts: 170 (0.09/day)
Thanks: 85
Thanked 7 Times in 6 Posts

System Specs

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
shuggans is offline  
Reply With Quote
Old Aug 14, 2011, 02:20 PM   #12
CrackerJack
2000 Posts
 
CrackerJack's Avatar
 
Join Date: Dec 2007
Location: East TN
Posts: 2,392 (1.20/day)
Thanks: 408
Thanked 439 Times in 342 Posts

System Specs

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
CrackerJack is offline  
Reply With Quote
The Following User Says Thank You to CrackerJack For This Useful Post:
Old Aug 14, 2011, 02:28 PM   #13
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,113 (5.28/day)
Thanks: 591
Thanked 5,492 Times in 2,936 Posts

System Specs

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
Kreij is online now  
Reply With Quote
The Following User Says Thank You to Kreij For This Useful Post:
Old Aug 14, 2011, 02:32 PM   #14
CrackerJack
2000 Posts
 
CrackerJack's Avatar
 
Join Date: Dec 2007
Location: East TN
Posts: 2,392 (1.20/day)
Thanks: 408
Thanked 439 Times in 342 Posts

System Specs

Quote:
Originally Posted by Kreij View Post
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.
ah yea i didn't catch that
CrackerJack is offline  
Reply With Quote
The Following User Says Thank You to CrackerJack For This Useful Post:
Old Aug 14, 2011, 08:15 PM   #15
shuggans
75 Posts
 
Join Date: Dec 2007
Location: Central Nebraska
Posts: 170 (0.09/day)
Thanks: 85
Thanked 7 Times in 6 Posts

System Specs

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
shuggans is offline  
Reply With Quote
Old Aug 14, 2011, 08:21 PM   #16
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,574 (6.29/day)
Thanks: 1,752
Thanked 2,596 Times in 1,960 Posts

System Specs

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(); }
FordGT90Concept is offline  
Crunching for Team TPU
Reply With Quote
The Following User Says Thank You to FordGT90Concept For This Useful Post:
Old Aug 14, 2011, 08:22 PM   #17
shuggans
75 Posts
 
Join Date: Dec 2007
Location: Central Nebraska
Posts: 170 (0.09/day)
Thanks: 85
Thanked 7 Times in 6 Posts

System Specs

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?
shuggans is offline  
Reply With Quote
Old Aug 14, 2011, 08:27 PM   #18
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,574 (6.29/day)
Thanks: 1,752
Thanked 2,596 Times in 1,960 Posts

System Specs

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(); }
FordGT90Concept is offline  
Crunching for Team TPU
Reply With Quote
The Following User Says Thank You to FordGT90Concept For This Useful Post:
Old Aug 14, 2011, 08:39 PM   #19
shuggans
75 Posts
 
Join Date: Dec 2007
Location: Central Nebraska
Posts: 170 (0.09/day)
Thanks: 85
Thanked 7 Times in 6 Posts

System Specs

there will be two separate txt files monitored in two separate text or combo boxes side by side.
shuggans is offline  
Reply With Quote
Old Aug 14, 2011, 08:46 PM   #20
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,113 (5.28/day)
Thanks: 591
Thanked 5,492 Times in 2,936 Posts

System Specs

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
Kreij is online now  
Reply With Quote
The Following User Says Thank You to Kreij For This Useful Post:
Old Aug 14, 2011, 09:09 PM   #21
shuggans
75 Posts
 
Join Date: Dec 2007
Location: Central Nebraska
Posts: 170 (0.09/day)
Thanks: 85
Thanked 7 Times in 6 Posts

System Specs

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
shuggans is offline  
Reply With Quote
Old Aug 14, 2011, 09:46 PM   #22
CrackerJack
2000 Posts
 
CrackerJack's Avatar
 
Join Date: Dec 2007
Location: East TN
Posts: 2,392 (1.20/day)
Thanks: 408
Thanked 439 Times in 342 Posts

System Specs

Please wrap your code

I believe i see one issue, updating VS atm... so gimme a few till i can post back
CrackerJack is offline  
Reply With Quote
The Following User Says Thank You to CrackerJack For This Useful Post:
Old Aug 14, 2011, 09:48 PM   #23
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,574 (6.29/day)
Thanks: 1,752
Thanked 2,596 Times in 1,960 Posts

System Specs

Quote:
Originally Posted by Kreij View Post
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.
__________________
Golden Rule of Programming: Never assume.

try { SteamDownload(); }
catch (Steamception ex) { RageQuit(); }
FordGT90Concept is offline  
Crunching for Team TPU
Reply With Quote
The Following User Says Thank You to FordGT90Concept For This Useful Post:
Old Aug 14, 2011, 09:54 PM   #24
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,113 (5.28/day)
Thanks: 591
Thanked 5,492 Times in 2,936 Posts

System Specs

Quote:
Originally Posted by FordGT90Concept View Post
If file.Length < lastoffset Then
' Start over
End If
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
Kreij is online now  
Reply With Quote
The Following User Says Thank You to Kreij For This Useful Post:
Old Aug 14, 2011, 09:57 PM   #25
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,574 (6.29/day)
Thanks: 1,752
Thanked 2,596 Times in 1,960 Posts

System Specs

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(); }
FordGT90Concept is offline  
Crunching for Team TPU
Reply With Quote
The Following User Says Thank You to FordGT90Concept For This Useful Post:
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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


All times are GMT. The time now is 07:16 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
no new posts