C
Craig Buchanan
I posted to this group, as I saw postings in the *.languages.vb suggesting
that this was the best place for threading questions.
I am attempting to write a component that watches a directory for file
additions (using FileSystemWatcher), adds the files to a queue, periodically
(using the Timer) copies the file in the queue to another location, then
finally deletes the original files. I was hoping to make the queue
processing/copying part of the application multi-threaded.
At this point, my application seems to works expected, except the file
deletion portion. Only the final file in the queue is deleted. For
example, if I copy 4 files to the montiored directory, all files are copied
to the new location, but only the last item in the queue is actually
deleted.
The relevant code sections are listed below.
I'd appreciate any thoughts on the matter. Thanks in advance.
Craig Buchanan
<code>
'triggered by the FileSystemWatcher
Private Sub OnCreated(ByVal source As Object, ByVal e As
FileSystemEventArgs)
_Queue.Enqueue(e.FullPath)
End Sub
'triggered by the timer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Do While _Queue.Count > 0
_RF = New FileMaker
Dim Thread As New System.Threading.Thread(AddressOf _RF.Export)
_RF.Source = _Queue.Dequeue
Thread.Start()
Loop
End Sub
'copys the file from one directory to another
Public Sub Export()
System.IO.File.Copy(Me.Source, Me.Source.Replace("inbound",
"outbound"))
RaiseEvent ExportComplete(Source)
End Sub
'handles the event that has been raised
Private Sub _RF_ExportComplete(ByVal Source As String) Handles
_RF.ExportComplete
System.IO.File.Delete(Source)
End Sub
</code>
that this was the best place for threading questions.
I am attempting to write a component that watches a directory for file
additions (using FileSystemWatcher), adds the files to a queue, periodically
(using the Timer) copies the file in the queue to another location, then
finally deletes the original files. I was hoping to make the queue
processing/copying part of the application multi-threaded.
At this point, my application seems to works expected, except the file
deletion portion. Only the final file in the queue is deleted. For
example, if I copy 4 files to the montiored directory, all files are copied
to the new location, but only the last item in the queue is actually
deleted.
The relevant code sections are listed below.
I'd appreciate any thoughts on the matter. Thanks in advance.
Craig Buchanan
<code>
'triggered by the FileSystemWatcher
Private Sub OnCreated(ByVal source As Object, ByVal e As
FileSystemEventArgs)
_Queue.Enqueue(e.FullPath)
End Sub
'triggered by the timer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Do While _Queue.Count > 0
_RF = New FileMaker
Dim Thread As New System.Threading.Thread(AddressOf _RF.Export)
_RF.Source = _Queue.Dequeue
Thread.Start()
Loop
End Sub
'copys the file from one directory to another
Public Sub Export()
System.IO.File.Copy(Me.Source, Me.Source.Replace("inbound",
"outbound"))
RaiseEvent ExportComplete(Source)
End Sub
'handles the event that has been raised
Private Sub _RF_ExportComplete(ByVal Source As String) Handles
_RF.ExportComplete
System.IO.File.Delete(Source)
End Sub
</code>