private void UnloadThread() { while (!_unloaded) { List <Chunk> chunksToUnload; lock (LoadedChunks) { chunksToUnload = LoadedChunks.Where( pair => DateTime.Now - pair.Value.Time > ChunkLifetime && !_chunksReadyToRemove.Contains(pair.Key)).Select(pair => pair.Value).ToList(); } foreach (var chunk in chunksToUnload) { WorldSerializer.SaveChunk(chunk); lock (_chunksReadyToRemove) { _chunksReadyToRemove.Add(chunk.Position); } } Thread.Sleep(1000); } }