The action can’t be completed because it is open in IIS Express Worker Process.

The action can’t be completed because it is open in IIS Express Worker Process.

I’ve been looking into an issue that is caused when saving an image to a local folder, the image is locked by an IIS Worker Process, meaning the delete functionality of the website always fails because the images is locked. If an image is deleted shortly after it is added, I found the lock could sometimes remain for up to 10 minutes. Displaying a message to the user stating that they need to wait to delete this image is just not acceptable.

Further investigation into the code, I wrapped any objects that implemented IDisposable in a using block in hope that resources will be cleaned up after execution and locks are removed, I could replicate this issue by using the website to add an image and then trying to rename the image manually in windows explorer.

I managed to track down a piece of code that looked like this, I could see here that there was no using block or any additional code calling dispose on the image instance.

var image = Image.FromFile(imagePath);

Changing this to include a using block allowed resources to be cleaned up and locks to be removed immediately.

using (var image = Image.FromFile(imagePath))

If you see the error above and you are unsure as to why the lock remains despite the IIS operation finishing, I would strongly suggest hunting through the code that saves the image and ensuring all objects are correctly disposed if they implement the IDisposable interface. This should then allow the IIS Worker Process to remove it’s lock immediately as memory is cleaned up, released and unmanaged resources are reset.