![]() |
|
|
#26 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,122 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts
|
@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
|
|
|
|
| The Following User Says Thank You to Kreij For This Useful Post: |
|
|
#27 |
![]() Join Date: Dec 2007
Location: East TN
Posts: 2,392 (1.20/day)
Thanks: 408
Thanked 439 Times in 342 Posts
|
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
|
|
|
|
| The Following User Says Thank You to CrackerJack For This Useful Post: |
|
|
#28 |
![]() Join Date: Dec 2007
Location: Central Nebraska
Posts: 170 (0.09/day)
Thanks: 85
Thanked 7 Times in 6 Posts
|
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.
|
|
|
|
|
|
#29 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,583 (6.28/day)
Thanks: 1,755
Thanked 2,599 Times in 1,962 Posts
|
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")
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
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
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
|
|
#30 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,122 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts
|
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
|
|
|
|
|
|
#31 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,583 (6.28/day)
Thanks: 1,755
Thanked 2,599 Times in 1,962 Posts
|
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(); } |
|
|
|
| The Following User Says Thank You to FordGT90Concept For This Useful Post: |
|
|
#32 | |
![]() Join Date: Dec 2007
Location: Central Nebraska
Posts: 170 (0.09/day)
Thanks: 85
Thanked 7 Times in 6 Posts
|
Quote:
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
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? |
|
|
|
|
|
|
#33 |
![]() Join Date: Dec 2007
Location: Central Nebraska
Posts: 170 (0.09/day)
Thanks: 85
Thanked 7 Times in 6 Posts
|
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?
|
|
|
|
|
|
#34 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,583 (6.28/day)
Thanks: 1,755
Thanked 2,599 Times in 1,962 Posts
|
1. Yes.
2. After updating the text... Code:
TextBox1.SelectionStart = TextBox1.Text.Length TextBox1.ScrollToCaret()
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
|
|
#35 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,583 (6.28/day)
Thanks: 1,755
Thanked 2,599 Times in 1,962 Posts
|
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(); } |
|
|
|
|
|
#36 |
![]() Join Date: Dec 2007
Location: Central Nebraska
Posts: 170 (0.09/day)
Thanks: 85
Thanked 7 Times in 6 Posts
|
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 |
|
|
|
|
|
#37 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,583 (6.28/day)
Thanks: 1,755
Thanked 2,599 Times in 1,962 Posts
|
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(); } |
|
|
|
![]() |
| 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 |