Homer said:
Here is the story:
Source and destination of each file is different in my case (or it can
be) and some are mapped drives (slow connection maybe). Some of those
files are huge (200Mb) and some very small (<5Kb). Unfortunately
delivering of small files is time sensitive. They come around 3:30PM
and need to be transferred for another process in less than two
minutes. Now imagine if I have one thread-program and have couple of
those big ones inside the queue and one small one behind all of them.
That's why I am interested in Multi-Threading in this case.
Ah, ok, this makes more sense.
So your requirement is that the system can be copying files happily, but
then notice a hi-priority file appears, and then pause the other
non-essential copying activity until the important stuff is done.
First idea that comes to mind:
Have two threads. The system can be copying normal files across from a
file queue in a 'main' thread. As soon as an important file appears, the
main thread pauses its activity (in mid file-copy, if need be)[1], and a
'priority' thread starts copying the important files across. When the
priority thread is finished, the 'main' thread can continue.
You could always have three threads - the extra one to be watching the
source folder, and controlling what the two copying threads get up to.
This would have the extra benefit of moving controlling logic out of a
file copier thread, and then your two copying threads ('main' and
'priority') could be instances of the same object, which copies files in
a file queue.
Of course, another option is just have one thread at any one time, and
give it the ability to remember where it was in the non-priority file
being copied. You could do this in a cheap and cheeky way by using
recursion (i.e. file copier calls itself when it notices a more
important file, and once done with that, it bottoms out and resumes the
original non-priority file). I don't think this is a good design though;
messy, with too many concerns all in one place.
[1] A file being paused in mid-copy may cause confusion - with partially
copied files existing and maybe messing things up. You may want to have
a policy of copying a file to a modified destination name, and only on
completion of the copy rename the file to its regular name.
For example, while a file 'data.txt' is being copied, it is copied to a
destination filename called 'data.txt-PARTIAL'. Upon copy completion,
the destination file is renamed by the program to 'data.txt'.
This way it's clear to humans (and to scripts, if need be) which are
non-complete files.