diff --git a/scripts/explorer-media-rename.ps1 b/scripts/explorer-media-rename.ps1 index 0b4b834..46324db 100644 --- a/scripts/explorer-media-rename.ps1 +++ b/scripts/explorer-media-rename.ps1 @@ -48,6 +48,7 @@ $QueueDir = Join-Path $env:LOCALAPPDATA 'SceneMarshal' $QueueFile = Join-Path $QueueDir 'queue.jsonl' $QueueLock = Join-Path $QueueDir 'queue.lock' $WorkerLock = Join-Path $QueueDir 'worker.lock' +$SpawnLock = Join-Path $QueueDir 'spawn.lock' $WorkerLog = Join-Path $QueueDir 'worker.log' $WorkerIdleSeconds = 20 $PwshPath = (Get-Process -Id $PID).Path @@ -161,8 +162,10 @@ function Invoke-Action([string]$itemAction, [string]$target) { # ---------------------------------------------------------------- worker 模式 function Invoke-Worker { + Write-WorkerLog 'worker 进程启动(抢锁中)' $workerLock = Enter-FileLock $WorkerLock 200 if ($null -eq $workerLock) { + Write-WorkerLog '已有 worker 在运行,本次进程直接退出' Write-Host '已有 worker 在运行,退出。' -ForegroundColor DarkGray return 0 } @@ -237,7 +240,17 @@ $target = Resolve-Target $Paths if ($Enqueue) { if (-not $target) { exit 0 } # 没拿到路径就静默退出,不弹窗 Add-QueueItem $Action $target - if (-not (Test-WorkerAlive)) { Start-Worker } + if (-not (Test-WorkerAlive)) { + # 多选时 Explorer 会同时发起 N 个入队调用,这里必须串行化, + # 否则每个调用都会在 worker 抢到锁之前各拉起一个窗口。 + $spawnLock = Enter-FileLock $SpawnLock 8000 + try { + if ($null -ne $spawnLock -and -not (Test-WorkerAlive)) { Start-Worker } + } + finally { + if ($null -ne $spawnLock) { $spawnLock.Close() } + } + } exit 0 }