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

Visual basic text file nightmare

Discussion in 'Programming & Webmastering' started by shuggans, Mar 28, 2011.

  1. shuggans

    Joined:
    Dec 22, 2007
    Messages:
    175 (0.07/day)
    Thanks Received:
    7
    Location:
    Central Nebraska
    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. temp02 New Member

    Joined:
    Mar 18, 2009
    Messages:
    493 (0.24/day)
    Thanks Received:
    166
    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.
     
    shuggans says thanks.
  3. W1zzard

    W1zzard Administrator Staff Member

    Joined:
    May 14, 2004
    Messages:
    14,909 (3.93/day)
    Thanks Received:
    11,668
    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
     
    shuggans says thanks.
  4. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,596 (6.24/day)
    Thanks Received:
    3,519
    Location:
    IA, USA
    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.
     
    shuggans says thanks.
    Crunching for Team TPU
  5. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.97/day)
    Thanks Received:
    5,616
    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
     
    shuggans and Akumos say thanks.
  6. Akumos

    Akumos

    Joined:
    Nov 30, 2008
    Messages:
    536 (0.25/day)
    Thanks Received:
    27
    Location:
    Birmingham, England...
    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!!!
     
    shuggans says thanks.
  7. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,596 (6.24/day)
    Thanks Received:
    3,519
    Location:
    IA, USA
    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.
     
    shuggans says thanks.
    Crunching for Team TPU
  8. CrackerJack

    CrackerJack

    Joined:
    Dec 13, 2007
    Messages:
    2,707 (1.09/day)
    Thanks Received:
    450
    Location:
    East TN
    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...
     
    shuggans says thanks.
  9. smartali89

    smartali89 New Member

    Joined:
    Dec 1, 2007
    Messages:
    525 (0.21/day)
    Thanks Received:
    13
    Location:
    Karachi, Pakistan
    Application.DoEvents()

    This might solve the problem, Which version of VB are you using?
     
    shuggans says thanks.
  10. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.97/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    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
     
    shuggans, Akumos and smartali89 say thanks.
  11. shuggans

    Joined:
    Dec 22, 2007
    Messages:
    175 (0.07/day)
    Thanks Received:
    7
    Location:
    Central Nebraska
    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: Aug 14, 2011
  12. CrackerJack

    CrackerJack

    Joined:
    Dec 13, 2007
    Messages:
    2,707 (1.09/day)
    Thanks Received:
    450
    Location:
    East TN
    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
     
    shuggans says thanks.
  13. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.97/day)
    Thanks Received:
    5,616
    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:
     
    shuggans says thanks.
  14. CrackerJack

    CrackerJack

    Joined:
    Dec 13, 2007
    Messages:
    2,707 (1.09/day)
    Thanks Received:
    450
    Location:
    East TN
    ah yea i didn't catch that :p
     
    shuggans says thanks.
  15. shuggans

    Joined:
    Dec 22, 2007
    Messages:
    175 (0.07/day)
    Thanks Received:
    7
    Location:
    Central Nebraska
    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. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,596 (6.24/day)
    Thanks Received:
    3,519
    Location:
    IA, USA
    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.
     
    shuggans says thanks.
    Crunching for Team TPU
  17. shuggans

    Joined:
    Dec 22, 2007
    Messages:
    175 (0.07/day)
    Thanks Received:
    7
    Location:
    Central Nebraska
    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. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,596 (6.24/day)
    Thanks Received:
    3,519
    Location:
    IA, USA
    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?
     
    shuggans says thanks.
    Crunching for Team TPU
  19. shuggans

    Joined:
    Dec 22, 2007
    Messages:
    175 (0.07/day)
    Thanks Received:
    7
    Location:
    Central Nebraska
    there will be two separate txt files monitored in two separate text or combo boxes side by side.
     
  20. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.97/day)
    Thanks Received:
    5,616
    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.
     
    shuggans says thanks.
  21. shuggans

    Joined:
    Dec 22, 2007
    Messages:
    175 (0.07/day)
    Thanks Received:
    7
    Location:
    Central Nebraska
    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. CrackerJack

    CrackerJack

    Joined:
    Dec 13, 2007
    Messages:
    2,707 (1.09/day)
    Thanks Received:
    450
    Location:
    East TN
    Please wrap your code :)

    I believe i see one issue, updating VS atm... so gimme a few till i can post back
     
    shuggans says thanks.
  23. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,596 (6.24/day)
    Thanks Received:
    3,519
    Location:
    IA, USA
    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.
     
    shuggans says thanks.
    Crunching for Team TPU
  24. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.97/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    What if the logfile was deleted and has grown longer than the offset already? :p
     
    shuggans says thanks.
  25. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,596 (6.24/day)
    Thanks Received:
    3,519
    Location:
    IA, USA
    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.
     
    shuggans says thanks.
    Crunching for Team TPU

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

Share This Page