RobertAkiraWatts

I'm having what is probably, given my skill level, an extremely stupid issue with trying to archive a file. Essentially, customers are copying files to an FTP site. I'm building a windows service that detects new files, does a whole mess of stuff is done with the data in the files, and then archives them using the following:

Code Snippet

public void ArchivePO(string name, string path, string destination)
{
try
{
DateTime dtNow = DateTime.Today;
string newName = dtNow.ToString("ddMMyy")+name;
FileInfo copyFile = new FileInfo(path);
copyFile.CopyTo(path + newName);
//this part doesn't work
FileInfo deleteFile = new FileInfo(path);
deleteFile.Delete();

}

catch

{

error handling

}

}

And I keep getting, The process cannot acces the file " . . . . . . " because it is being used by another process. I know there is some simple and stupid thing that I'm missing here, but I've yet to figure it out. Anyone have any thoughts



Re: Visual C# General FileInfo.Delete question

Peter Ritchie

If you're running as a service, how are you detecting new/changed files If you're using FileSystemWatcher it will inform you of changes while they are occurring; so you will likely not be able to read the entire file (e.g. you may be informed of a write while a series of writes are occurring...). There's a been a couple of threads about this:

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1012208&SiteID=1
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=458958&SiteID=1

Each thread outlines using Sleep to pause between trying to access the file. I don't recommend using sleep because it forces your thread to be alive for at least that amount of time; plus another high-priority thread could starve that thread of CPU. I would recommend performing the check on a background thread and using a synchronization object and call it's WaitOne method with a timeout. That way, the main thread can simply set that object to force the waiting thread to wake up (and exit, usually).






Re: Visual C# General FileInfo.Delete question

RobertAkiraWatts

You're correct in assuming that I'm using FileSystemWatcher to detect new files. (I'm not interested in changed files.) And yes, I also used Sleep to pause the thread before trying to do anything with the file. It sounds as though that may have been the wrong approach. Are you saying that I should use FileSystemWatcher on a background thread, and then have the bulk of the work happen on the main thread



Re: Visual C# General FileInfo.Delete question

Peter Ritchie

I'm saying you should pause your thread with a method that is interruptible. Sleep isn't interruptible without using exceptions, which I don't recommend. Something like ManualResetEvent can provide the same effect as Sleep without the potential consequences. For example, instead of Sleep you could do:

Code Snippet
ManualResetEvent manualResetEvent = new ManualResetEvent(false);
// ...
manualResetEvent.WaitOne(1000, false);

The benefit of that is another thread can interrupt that pause by simply manualResetEvent.Set();

But, in either case, in order to interrupt the thread waiting you must do it from another, running, thread. This means the work (data/file manipulation and waiting) needs to be done a background thread. In the case of a service, when you shut down your service cannot be terminated for as much as 1000 ms (one second), and potentially more if there are many high-priority threads running (which there tends to be when shutting down).






Re: Visual C# General FileInfo.Delete question

RobertAkiraWatts

Thanks. What you're suggesting seems just within my grasp, given my current skill level. Which should make following your suggestion a nice learning experience. Much obliged.