Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions Helpers/UpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ private static bool IsPrereleaseVersion(string version) =>

private UpdateManager? _manager;

/// <summary>
/// 全局下载锁,防止多个 UpdateService 实例并发执行下载导致 Velopack 锁文件冲突。
/// </summary>
private static readonly SemaphoreSlim _globalDownloadLock = new(1, 1);

/// <summary>
/// 获取当前应用版本(Velopack 优先,回退到程序集版本)
/// </summary>
Expand Down Expand Up @@ -165,15 +170,30 @@ public static string GetCurrentVersion()
}

/// <summary>
/// 下载并应用更新,完成后重启应用
/// 下载并应用更新,完成后重启应用。
/// 使用全局锁确保同一时刻只有一个下载操作在运行,避免 Velopack 锁文件冲突。
/// </summary>
public async Task DownloadAndApplyAsync(UpdateInfo update, Action<int>? onProgress = null)
{
if (_manager is null)
throw new InvalidOperationException("请先调用 CheckForUpdateAsync");

await _manager.DownloadUpdatesAsync(update, onProgress);
Log.Information("更新下载完成,准备重启");
_manager.ApplyUpdatesAndRestart(update);
var acquired = await _globalDownloadLock.WaitAsync(TimeSpan.Zero);
if (!acquired)
{
Log.Warning("已有更新操作正在进行,跳过本次下载");
return;
}
Comment on lines +181 to +186

try
{
await _manager.DownloadUpdatesAsync(update, onProgress);
Log.Information("更新下载完成,准备重启");
_manager.ApplyUpdatesAndRestart(update);
}
finally
{
_globalDownloadLock.Release();
}
}
}
2 changes: 1 addition & 1 deletion ViewModels/UpdateViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ partial void OnSelectedUpdateChannelChanged(int value)
[RelayCommand]
private async Task CheckForUpdateAsync()
{
if (IsChecking) return;
if (IsChecking || IsUpdating) return;
IsChecking = true;
Comment on lines +55 to 56
StatusMessage = "正在检查更新...";

Expand Down
Loading