private void MaintainOpenedFileCount() { if (MruManager.Count <= MaxInstanceCount) { return; } while (MruManager.Count > MruManager.MinCapacity) { Mru.Generic.MruItem <int, FileStream> item = MruManager.PeekInTail(); if (item.Value.InUse) { return; } lock (item.Value._locker) { if (item.Value.InUse || item.Value._fileStream == null) { return; } MruManager.Remove(item.Value.Id); //item.Value._streamPosition = item.Value._fileStream.Position; item.Value._fileStream.Flush(); item.Value._fileStream.Close(); item.Value._fileStream = null; } } }
private static void MaintainOpenedFileCount(bool closeAll = false) { if (MruManager.Count <= MaxInstanceCount) { return; } lock (MruManager) { int ctr = 0; while (closeAll || MruManager.Count > MruManager.MinCapacity) { if (ctr++ >= MruManager.MaxCapacity - 10) { Log.Logger.Instance.Verbose("MaintainOpenedFileCount FileStream trimming loop reached {0}, ending trimming...", ctr); break; } Mru.Generic.MruItem <int, FileStream> item = MruManager.PeekInTail(); //if (item.Value.InUse) return; lock (item.Value._locker) { if (MruManager.Count <= MruManager.MinCapacity) { if (item.Value._fileStream == null) { MruManager.Remove(item.Value.Id); return; } if (item.Value.InUse) { return; } } if (item.Value.InUse) { item.Value.InUse = true; continue; } //item.Value._streamPosition = item.Value._fileStream.Position; if (item.Value._fileStream != null || !item.Value._isDisposed) { item.Value.InUse = true; if (item.Value._fileStream != null || !item.Value._isDisposed) { try { item.Value._fileStream.Flush(); if (item.Value._fileStream != null) { item.Value._fileStream.Close(); } } catch { } } item.Value._fileStream = null; MruManager.Remove(item.Value.Id); item.Value.InUse = false; } else { MruManager.Remove(item.Value.Id); } } } } }