improve:安全面板

This commit is contained in:
2026-02-19 18:28:01 +08:00
parent d2abadd69b
commit b16ec37df3
2 changed files with 66 additions and 11 deletions
+29 -4
View File
@@ -64,11 +64,20 @@ namespace Ink_Canvas.Helpers
return;
}
var waitStart = Environment.TickCount;
while (Interlocked.CompareExchange(ref _writeGate, 1, 0) != 0)
const int gateTimeoutMs = 10_000;
if (!TryEnterWriteGate(gateTimeoutMs))
{
if (Environment.TickCount - waitStart < 2000) Thread.Sleep(10);
else Thread.Sleep(50);
try
{
LogHelper.WriteLogToFile($"ProcessProtectionManager.WithWriteAccess: 获取写入门闩超时({gateTimeoutMs}ms),将降级直接执行写入动作。目标: {targetPath}",
LogHelper.LogType.Warn);
}
catch
{
}
action();
return;
}
var normalized = NormalizePath(targetPath);
@@ -134,6 +143,22 @@ namespace Ink_Canvas.Helpers
}
}
private static bool TryEnterWriteGate(int timeoutMs)
{
if (timeoutMs <= 0) timeoutMs = 1;
var start = Environment.TickCount;
while (Interlocked.CompareExchange(ref _writeGate, 1, 0) != 0)
{
var elapsed = unchecked(Environment.TickCount - start);
if (elapsed >= timeoutMs) return false;
if (elapsed < 2000) Thread.Sleep(10);
else Thread.Sleep(50);
}
return true;
}
private static void Enable()
{
Enable(rescanRoot: true, rescanDirs: null);