diff --git a/src/Starward/Services/Download/InstallGameService.cs b/src/Starward/Services/Download/InstallGameService.cs index dcfb4f570..8a8bd84a3 100644 --- a/src/Starward/Services/Download/InstallGameService.cs +++ b/src/Starward/Services/Download/InstallGameService.cs @@ -627,6 +627,8 @@ public void ClearState() protected void StartTask(InstallGameState state) { + if (_concurrentExecuteThreadCount > 0) return; + if (state is InstallGameState.Download) { if (InstallTask is InstallGameTask.HardLink) @@ -713,10 +715,26 @@ protected void StartTask(InstallGameState state) _finishBytes = 0; } State = state; - _cancellationTokenSource = new CancellationTokenSource(); - for (int i = 0; i < Environment.ProcessorCount; i++) + + _ = RunTasksAsync(); //不需要ConfigureAwait,因为返回值丢弃,且无需调用“.GetAwaiter().OnCompleted()” + return; + + async Task RunTasksAsync() { - _ = ExecuteTaskItemAsync(_cancellationTokenSource.Token).ConfigureAwait(false); + _cancellationTokenSource = new CancellationTokenSource(); + var tasks = new Task[Environment.ProcessorCount]; + for (int i = 0; i < Environment.ProcessorCount; i++) + { + tasks[i] = ExecuteTaskItemAsync(_cancellationTokenSource.Token); + } + try + { + await Task.WhenAll(tasks).ConfigureAwait(false); + } + finally + { + CurrentTaskFinished(); + } } } @@ -1106,11 +1124,6 @@ protected async Task ExecuteTaskItemAsync(CancellationToken cancellationToken = try { Interlocked.Increment(ref _concurrentExecuteThreadCount); - if (_concurrentExecuteThreadCount > Environment.ProcessorCount) - { - return; - } - while (_installItemQueue.TryDequeue(out InstallGameItem? item)) { try @@ -1161,10 +1174,6 @@ protected async Task ExecuteTaskItemAsync(CancellationToken cancellationToken = finally { Interlocked.Decrement(ref _concurrentExecuteThreadCount); - if (_concurrentExecuteThreadCount == 0) - { - CurrentTaskFinished(); - } } }