I've created a program that moves files to and from various directories. An issue I've come across is when you're trying to move a file and some other program is still using it. And you get an error. Leaving it there isn't an option, so I can only think of having to keep trying to move it over and over again. This though slows the entire program down, so I create a new thread and let it deal with the problem file and move on to the next. The bigger problem is when you have too many of these problem files and the program now has so many threads trying to move these files, that it just crashes with some kernel.dll error. Here's a sample of the code I use to move the files:
Public Sub MoveIt()
Try
File.Move(_FileName, _CopyToFileName)
Catch ex As Exception
Threading.Thread.Sleep(5000)
MoveIt()
End Try
End Sub
As you can see.. I try to move the file, and if it errors, I wait and move it again.. over and over again.. I've tried using FileInfo as well, but that crashes WAY sooner than just using the File object.
So has anyone found a fool proof way of moving files without it ever erroring?
Note: it takes a lot of files to make it crash. It'll be fine on the weekend, but by the end of the day on monday, it's done.
UPDATE!!!
I appreciate all the ideas so far. Perhaps I should give more information about what I'm doing.
This is all done in a windows service. The files MUST be moved. There's no way I can leave any behind. which is why I must try OVER and OVER again to move these files. The files are used to import data into various databases. Plus there is NO user to tell if the file cannot be moved. Also, this program processes THOUSANDS of files a day.
So with that said. How can I have an efficient program that can move files without any user interaction, and guarantee that all the files get moved? the programs that create these files eventually give up their hold on them. They get created by FTP, Biztalk and other various services.