techPowerUp! Forums

Go Back   techPowerUp! Forums > Software > Programming & Webmastering

Reply
 
Thread Tools
Old Aug 14, 2011, 10:06 PM   #26
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,122 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts

System Specs

@Shuggans,
Sorry for going off-topic. lol
It looks like you are starting a thread but still running the loop in your main method.
__________________

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, 10:14 PM   #27
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

Still can't figure it out, But you have the thread start wrong....

Code:
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
'Thread start should be here, other wise it's repeat it self over and over till stop
readlog() 
'Dim Thread_UploadLogReader As New System.Threading.Thread(AddressOf readlog)
'Thread_UploadLogReader.Start()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
Public Sub readlog()
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
CrackerJack is offline  
Reply With Quote
The Following User Says Thank You to CrackerJack For This Useful Post:
Old Aug 14, 2011, 11:11 PM   #28
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

Gahhh

Ive messed with this for so long. Is this even possible to do in a windows form? Moving the thread start to the button click event produced the same results.
shuggans is offline  
Reply With Quote
Old Aug 14, 2011, 11:27 PM   #29
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,583 (6.28/day)
Thanks: 1,755
Thanked 2,599 Times in 1,962 Posts

System Specs

I wrote a class quickly to do what you are asking for with an example form of how to implement it...


Basically, add MonitorFileForChanges.vb to your project then, for each file you want to monitor, do something like:

Code:
Private WithEvents file1 As New MonitorFileForChanges("Path to file1")
Private WithEvents file2 As New MonitorFileForChanges("Path to file2")
Private WithEvents file3 As New MonitorFileForChanges("Path to file3")
Next, you'll have to start the workers. This is done by simply calling file1.StartWorker(), file2.StartWorker(), and file3.StartWorker().

The toughest part is avoiding that nasty cross-thread reference. The code below basically detects if a thread that isn't itself is accessing the method and if so, it runs it from itself (yeah, that's complex). I bolded the bits that need to change for each one. You should use more descriptive names than file1, file2, and file3, obviously:

Code:
    Public Sub File1_LineAdded(ByVal line) Handles file1.LineAdded
        If Me.InvokeRequired Then
            Me.Invoke(New LineAddedHandler(AddressOf File1_LineAdded), line)
        Else
            ListBox1.Items.Add(line)
        End If
    End Sub

    Public Sub File2_LineAdded(ByVal line) Handles file2.LineAdded
        If Me.InvokeRequired Then
            Me.Invoke(New LineAddedHandler(AddressOf File2_LineAdded), line)
        Else
            ListBox2.Items.Add(line)
        End If
    End Sub

    Public Sub File3_LineAdded(ByVal line) Handles file3.LineAdded
        If Me.InvokeRequired Then
            Me.Invoke(New LineAddedHandler(AddressOf File3_LineAdded), line)
        Else
            ListBox3.Items.Add(line)
        End If
    End Sub
Finally, those worker threads work in a more-or-less infinite loop. This means you have to stop them or else your application won't close when it is told to close. You can do this by creating a handle to the FormClosing event and then call file1.StopWorker(), file2.StopWorker(), and file3.StopWorker() like so:

Code:
    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        file1.StopWorker()
        file2.StopWorker()
        file3.StopWorker()
    End Sub
Now, if a line is added to any of three files, it will show up in ListBox1-3.
Attached Files
File Type: zip monitor_file_for_changes.zip (77.7 KB, 32 views)
__________________
Golden Rule of Programming: Never assume.

try { SteamDownload(); }
catch (Steamception ex) { RageQuit(); }
FordGT90Concept is online now  
Crunching for Team TPU
Reply With Quote
The Following 2 Users Say Thank You to FordGT90Concept For This Useful Post:
Old Aug 14, 2011, 11:29 PM   #30
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,122 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts

System Specs

Nice Ford. I thought you quit coding for awhile to play games.
__________________

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
Old Aug 14, 2011, 11:30 PM   #31
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,583 (6.28/day)
Thanks: 1,755
Thanked 2,599 Times in 1,962 Posts

System Specs

Beyond Good & Evil lost its pull on me.

3 more days to From Dust...
__________________
Golden Rule of Programming: Never assume.

try { SteamDownload(); }
catch (Steamception ex) { RageQuit(); }
FordGT90Concept is online now  
Crunching for Team TPU
Reply With Quote
The Following User Says Thank You to FordGT90Concept For This Useful Post:
Old Aug 14, 2011, 11:40 PM   #32
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

Quote:
Originally Posted by temp02 View Post
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.
Wow. Just tried abandoning my code and trying this...

Code:
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

    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 100
        Timer1.Start()
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Using reader As New StreamReader(New FileStream("C:\logs\UploadLog.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            TextBox1.Text = reader.ReadToEnd
        End Using

    End Sub


End Class
And it works beautifully. Sorry to take up your time Ford :s.
Two questions with this though
1. Am I correct in thinking each timer uses and starts its own thread automatically?
2. How do I lock the scrollbars on a textbox to the bottom of the textbox?
shuggans is offline  
Reply With Quote
Old Aug 15, 2011, 12:16 AM   #33
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

Question about multithreading in vb.net

When I start A new thread for a subroutine to run on, should I start the new thread in that subroutine, or in the event that calls the subroutine?
shuggans is offline  
Reply With Quote
Old Aug 15, 2011, 12:26 AM   #34
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,583 (6.28/day)
Thanks: 1,755
Thanked 2,599 Times in 1,962 Posts

System Specs

1. Yes.
2. After updating the text...
Code:
TextBox1.SelectionStart = TextBox1.Text.Length
TextBox1.ScrollToCaret()
Just beware that ReadToEnd() and using a TextBox will start to slow down after about 32 KiB of data are read.
__________________
Golden Rule of Programming: Never assume.

try { SteamDownload(); }
catch (Steamception ex) { RageQuit(); }
FordGT90Concept is online now  
Crunching for Team TPU
Reply With Quote
Old Aug 15, 2011, 12:32 AM   #35
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,583 (6.28/day)
Thanks: 1,755
Thanked 2,599 Times in 1,962 Posts

System Specs

The form is always on the "main" thread. It will own the thread(s) it starts.

Usually you don't start threads inside of threads unless they are doing something special/different.

You need to start work threads from somewhere in the main thread. It doesn't particularlly matter where.

You'll get circular logic if you try to start a thread from inside itself. In fact, it would probably never even start in the first place.
__________________
Golden Rule of Programming: Never assume.

try { SteamDownload(); }
catch (Steamception ex) { RageQuit(); }
FordGT90Concept is online now  
Crunching for Team TPU
Reply With Quote
Old Aug 21, 2011, 10:07 PM   #36
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

Write to a text file without streamwriter?

Ok. This is the last piece holding me up.
Is there ANY way I can write to a text file without locking it in use? Im currently using streamwriter- I need a different method to do so. Is this possible? would using a cmd to append a string be the best way, and if so is there a way to do it without causing a CMD window to pop up?? and what are some more alternatives?
Thanks in advance
shuggans is offline  
Reply With Quote
Old Aug 21, 2011, 11:10 PM   #37
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,583 (6.28/day)
Thanks: 1,755
Thanked 2,599 Times in 1,962 Posts

System Specs

When you make a new FileStream, one of the later options in it is the file locks.
http://msdn.microsoft.com/en-us/library/ms143397.aspx

FileShare is the one you need to modify:
http://msdn.microsoft.com/en-us/libr...fileshare.aspx

Example (substitute "StreamWriter" for "StreamReader"):
http://balajiramesh.wordpress.com/20...the-file-in-c/


If that doesn't work then yeah, you'll have to look into cmd options.
__________________
Golden Rule of Programming: Never assume.

try { SteamDownload(); }
catch (Steamception ex) { RageQuit(); }
FordGT90Concept is online now  
Crunching for Team TPU
Reply With Quote
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 04:17 PM.


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