Greetings.

In my Console Application, I have a process that connects to a Access Database File. After closing the connection I copy the file to another folder, and delete the original.

Lately this is failing a lot. For some reason the File is still being used, even if the connection is closed.

I think that the problem is the speed at which it's doing everything, I try to delete it right after the closeConnection() method. And the connection might not be fully closed??? This is something that I don't fully understand yet.

Anyways, I wanted to add a small timer (this application doesn't have to run with a time constraint so I don't mind waiting).

I've read here on the forums that people use Thread.Sleep().

Is that safe? It seemed to work when I tested it. But is this the correct way to do it? Does anyone have any insight or any different idea on how to fix this "File currently in use" problem?

Regards to all,

CB.

EDIT: I don't use threads, nor tasks... The code execution is linear. .NET 4.0

Edited 4 Years Ago by charlybones: Added info

Could try running it with a sleep of (500), equivalent of half a second.


Thread.Sleep() Is safe as it literally pauses execution of the thread and so therefore just makes the computer wait before running your next line of code, might give it the time to close the connection properly though in personal experience i've never had your issue.

well alternatively you can use a while loop :P

the while loop condition checks if the file exists

then within the while loop put a try catch and then try delete the file, if its still locked the exception will catch it, and will constantly try and delete it till its removed.

Thats one way I can think of, which includes a lot of overhead most likely, but will work.

A busy wait loop will task the CPU while putting the thread to sleep will not. If you need a short sleep to let the connection close completely then Thread.Sleep() is the way to do it.

I would say a combination of the loop checking if the file is available, with a thread.sleep() in the loop would do best. Make sure the Thread.Sleep() is only about 200ms to prevent unneeded code blocking.

Thanks for all the replies, I still am not able to figure out at what times the File.Delete() fails... As it happens with some files and not with others.

Anyways, thank you all for the replies.

Thread solved!

I still am not able to figure out at what times the File.Delete() fails... As it happens with some files and not with others.

It's not necessarily the files themselves, but processes holding an open handle to them. I've had luck with the following type of class in cases like this:

public static class SafeFile
{
    public static void Delete(string path)
    {
        if (File.Exists(path))
        {
            File.Delete(path);
        }
    }

    public static void TimedDelete(string path, int msPause)
    {
        if (!File.Exists(path))
        {
            return;
        }

        for (int tries = 0; tries < 3; tries++)
        {
            try
            {
                Thread.Sleep(msPause);
                File.Delete(path);

                // No exceptions; assume successful deletion
                break;
            }
            catch
            {
                // Try again
            }
        }
    }
}

Even though this is solved, I figuired I would add to it.

Don't use Thread.Sleep() on your main (UI) thread. Instead, create a new thread to do things that might block your code (such as IO/networking/etc).

This question has already been answered. Start a new discussion instead.