Ok, the storyline: A web site creates a temporary file to work with, then is supposed to delete it, because maybe space is low on the drive or whatever. There are times where the file will still be locked from our work when we try to delete it. Perhaps we did something like:
And it hasn’t released the lock on “oldFile” when we get to the next statement. Oh man, that’s a problem. A BIG problem. A HUGE problem.
Not anymore! Introducing a stupid anonymous thread method pause trick we can do:
bool deleted = false;
int tries = 3;
while ((!deleted) && (tries > 0))
deleted = true;
Thread pauser = new Thread(
while (pauser.IsAlive) ;
Oh god yeah…
Ok, a quick run-through. We try to delete our file. An exception is caught, so to allow the current thread to finish any IO operations, we create a temporary thread (pauser), which we wait for to finish. Once it’s done, we try again until either we succeed or just give up.
My sandbox for playing around with this idea is here. I have a question, though, that needs answering desperately:
Is this useful anywhere?
If so, please provide an example of where this would be great to use. I just need to know I didn’t fully waste my time.