Exemplo n.º 1
0
        private static FileStreamBlockCollection CreateCollection(IDatabaseFileProvider databaseFileProvider, bool writable, FileMode mode, FileAccess access, FileShare share)
        {
            var endTime = DateTime.Now + databaseFileProvider.Timeout;

            var @lock = writable ?
                        Scheduler.EnterWrite(databaseFileProvider.Path, databaseFileProvider.Timeout)
                : Scheduler.EnterRead(databaseFileProvider.Path, databaseFileProvider.Timeout);

            while (true)
            {
                try
                {
                    var stream = new FileStream(databaseFileProvider.Path, mode, access, share);
                    return(new FileStreamBlockCollection(stream, @lock));
                }
                catch (FileNotFoundException)
                {
                    EnsureFile(databaseFileProvider.Path);
                }
                catch (IOException e)
                {
                    if (IoError.IsSharingViolation(e))
                    {
                        if (endTime > DateTime.Now)
                        {
                            databaseFileProvider.HandleSharingViolation();
                            continue;
                        }
                    }
                    @lock.Dispose();
                    throw;
                }
                catch (Exception)
                {
                    @lock.Dispose();
                    throw;
                }
            }
        }