improve:安全面板
This commit is contained in:
@@ -64,11 +64,20 @@ namespace Ink_Canvas.Helpers
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var waitStart = Environment.TickCount;
|
const int gateTimeoutMs = 10_000;
|
||||||
while (Interlocked.CompareExchange(ref _writeGate, 1, 0) != 0)
|
if (!TryEnterWriteGate(gateTimeoutMs))
|
||||||
{
|
{
|
||||||
if (Environment.TickCount - waitStart < 2000) Thread.Sleep(10);
|
try
|
||||||
else Thread.Sleep(50);
|
{
|
||||||
|
LogHelper.WriteLogToFile($"ProcessProtectionManager.WithWriteAccess: 获取写入门闩超时({gateTimeoutMs}ms),将降级直接执行写入动作。目标: {targetPath}",
|
||||||
|
LogHelper.LogType.Warn);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
action();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var normalized = NormalizePath(targetPath);
|
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()
|
private static void Enable()
|
||||||
{
|
{
|
||||||
Enable(rescanRoot: true, rescanDirs: null);
|
Enable(rescanRoot: true, rescanDirs: null);
|
||||||
|
|||||||
@@ -1388,9 +1388,19 @@ namespace Ink_Canvas
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void Window_Closing(object sender, CancelEventArgs e)
|
private bool _allowCloseAfterExitVerification;
|
||||||
|
private bool _isExitVerificationInProgress;
|
||||||
|
|
||||||
|
private void Window_Closing(object sender, CancelEventArgs e)
|
||||||
{
|
{
|
||||||
LogHelper.WriteLogToFile("Ink Canvas closing", LogHelper.LogType.Event);
|
LogHelper.WriteLogToFile("Ink Canvas closing", LogHelper.LogType.Event);
|
||||||
|
|
||||||
|
if (_allowCloseAfterExitVerification)
|
||||||
|
{
|
||||||
|
_allowCloseAfterExitVerification = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// 快抽按钮现在集成在主窗口中,不需要单独关闭
|
// 快抽按钮现在集成在主窗口中,不需要单独关闭
|
||||||
@@ -1404,13 +1414,33 @@ namespace Ink_Canvas
|
|||||||
{
|
{
|
||||||
if (!App.IsUpdateInstalling && SecurityManager.IsPasswordRequiredForExit(Settings))
|
if (!App.IsUpdateInstalling && SecurityManager.IsPasswordRequiredForExit(Settings))
|
||||||
{
|
{
|
||||||
bool ok = await SecurityManager.PromptAndVerifyAsync(Settings, this, "退出验证", "请输入安全密码以退出软件。");
|
e.Cancel = true;
|
||||||
if (!ok)
|
if (_isExitVerificationInProgress) return;
|
||||||
|
|
||||||
|
_isExitVerificationInProgress = true;
|
||||||
|
Dispatcher.BeginInvoke(new Action(async () =>
|
||||||
{
|
{
|
||||||
e.Cancel = true;
|
try
|
||||||
LogHelper.WriteLogToFile("Ink Canvas closing cancelled by security password", LogHelper.LogType.Event);
|
{
|
||||||
return;
|
bool ok = await SecurityManager.PromptAndVerifyAsync(Settings, this, "退出验证", "请输入安全密码以退出软件。");
|
||||||
}
|
if (!ok)
|
||||||
|
{
|
||||||
|
LogHelper.WriteLogToFile("Ink Canvas closing cancelled by security password", LogHelper.LogType.Event);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_allowCloseAfterExitVerification = true;
|
||||||
|
Close();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_isExitVerificationInProgress = false;
|
||||||
|
}
|
||||||
|
}), DispatcherPriority.Normal);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
|
|||||||
Reference in New Issue
Block a user