Quote:
That would clearly handle the lock, but what about the waiting until it is unlocked part?
Interestingly, the documentation is silent on what happens if the file cannot be locked. It implies that it'll block indefinitely.
However, one thing that occurs -- this is a web app, yes? In which case, it'll probably be running in a single ASP.NET helper process, which means that process-afinity locks are no use to you.
On the other hand, since this is a single process, you don't need the mutex, either. You can simply stash an object ("FileArbiter" maybe?) in the HTTP Application state, which you can then get at from the Page methods.
The HttpApplicationState object is available from Page.Application, or HttpContext.Current.Application.
You can then do something like the the following:
In Global.asax, in Application_OnStart:
this.Application["FileArbiter"] = new FileArbiter();
In wherever you need to lock the file:
FileArbiter fileArbiter = this.Application["FileArbiter"];
try
{
fileArbiter.TryLock(filename, timeout);
// something interesting.
}
finally
{
fileArbiter.Unlock(filename);
}
...or something like that.
However, there's something in the documentation about "a pool of ... HttpApplication instances." This worries me, because it implies that a web application might not have a single HttpApplication object.
This would be stupid, so I suggest you dig a little more around this area, to see if you can clarify the lifetime/scope of this object.
_________________________
--
roger