Threading question (newbie)

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>
 
G

Guest

I'm not sure why your code isn't working properly but I will give 2
suggestions:

1) Spawning a new thread each time you get a file from the queue isn't
necessarily a good implementation of "Multithreaded". If you have a large
number of files, you're going to be creating a lot of threads. Better to
QueueUserWorkItem onto the ThreadPool with a callback. Threapool will take
care of cleanup and recycle threads that are done back to an available state.
2) I'm not sure you really need to do your delete in an event handler. If
you're finished copying the renamed file, then why not just add one more line
of code and go ahead and delete the little booger right then and there?

Cheers
Peter
 
C

Craig Buchanan

Peter-

Thanks for the reply. My next question was going to be about pooling or
limiting the number of threads. Obviously, the optimal number of threads is
going to be dependent upon the application and the system's resources, but
tell me what to look for when trying to determine an appropriate number of
threads in a pool? processor usage, disk usage? are there .Net framework
components that can be invoked to analyze resource allocation?

Thanks,

Craig
 
C

Craig Buchanan

I was able to get the QueueUserWorkItem working--it does exactly what I
want. Thanks for the tip!
 
B

Brian Gideon

I was able to get the QueueUserWorkItem working--it does exactly what I
want. Thanks for the tip!

Are you using the FileSystemWatcher on a windows forms or is the
SynchronizingObject property set? The FileSystemWatcher will execute
event handlers on a ThreadPool automatically if the property is not
set.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top