File in use by another process.

TomPhillips

Active member
Joined
Feb 24, 2005
Messages
33
Programming Experience
10+
File in use by another process. RESOLVED

I have an app that moves files around, renames, selects and deletes JPEGs. It uses a thread to copy groups of up to 200 files of 50 to 100K each. While The thread is running, it updates a form to let the user know when a group of files has been copied to the work area, so they can start "processing" them. Processing means displaying some in a list view for selection and deletion. Then the an explorer window is opened so that groups of files can be "dragged" into another application for image manipulation.

Frequently, I get the error "File in use by another process..." even though the "background" thread is finished with the named file. I have been told that this is a "garbage collection" issue.

So do I need a "System.GC.collect() after each
System.IO.File.Copy(Source, Destination, True)?

Or is there a better way to release a file imediately that the process is done with?
 
Last edited:
You should almost never have to explicitly initiate garbage collection. It is, in relative terms, a very expensive operation and there are very few cases that require it.

There is a good chance that you are not disposing some of the objects that you are using. Many classes have a Dispose method that releases the resources held by instances of that class. If you simply let a variable that refers to one of these objects fall out of scope, then you may not be releasing those resources. A good example of this is the Image class. If you create an Image object using Image.FromFile, the file that the Image was created from remains locked until you call Dispose on the object. Check all the variables you are using and if any of them have a Dispose method then you should call it before the variable loses scope. Some people like to set the variable to Nothing as well. This does no harm but is unnecessary so, personally, I don't bother.
 
Back
Top