private static void Execute() { DateTime now = DateTime.Now; TimeSpan timeout = Initializer.SessionConfig.Timeout.Add(TimeSpan.FromMinutes(5)); // 多加 5 分钟 StringBuilder s = new StringBuilder(); foreach (string filePath in Directory.GetFiles(Initializer.TempPath, "*.dat")) { if (File.Exists(filePath) == false) { continue; } FileInfo file = new FileInfo(filePath); bool delete = file.LastAccessTime.Add(timeout) < now; if (delete) { s.AppendLine(filePath); RetryFile.Delete(filePath); // 删除内存中的用户锁对象,以免内存泄露 string sessionId = FileStore.GetSessionId(filePath); UserLock.XInstance.RemoveLock(sessionId); } } if (s.Length > 0) { string message = "XSession.Modules.FileCleanTask 已清理文件:\r\n" + s.ToString(); WinLogger.WriteInfo(message); } }
public static void DeleteFile(string sessionId) { string filePath = GetSessionFilePath(sessionId); object lockObject = SidLock.Instance.GetLock(sessionId); lock ( lockObject ) { if (File.Exists(filePath)) { RetryFile.Delete(filePath); } } SidLock.Instance.RemoveLock(sessionId); }