Skip to content

Commit

Permalink
fixed the bug that method CurrentTaskFinished was executed multiple…
Browse files Browse the repository at this point in the history
… times by multiple threads (Scighost#1083) (Scighost#1082)

Signed-off-by: OsakaRuma <[email protected]>
  • Loading branch information
iamscottxu committed Sep 8, 2024
1 parent 37253b8 commit 57da19b
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/Starward/Services/Download/InstallGameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -713,10 +713,20 @@ 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 tsaks = new Task[Environment.ProcessorCount];
for (int i = 0; i < Environment.ProcessorCount; i++)
{
tsaks[i] = ExecuteTaskItemAsync(_cancellationTokenSource.Token);
}
await Task.WhenAll(tsaks).ConfigureAwait(false);
CurrentTaskFinished();
}
}

Expand Down Expand Up @@ -1105,8 +1115,8 @@ protected async Task ExecuteTaskItemAsync(CancellationToken cancellationToken =
{
try
{
Interlocked.Increment(ref _concurrentExecuteThreadCount);
if (_concurrentExecuteThreadCount > Environment.ProcessorCount)
var concurrentExecuteThreadCount = Interlocked.Increment(ref _concurrentExecuteThreadCount);
if (concurrentExecuteThreadCount > Environment.ProcessorCount)
{
return;
}
Expand Down Expand Up @@ -1161,10 +1171,6 @@ protected async Task ExecuteTaskItemAsync(CancellationToken cancellationToken =
finally
{
Interlocked.Decrement(ref _concurrentExecuteThreadCount);
if (_concurrentExecuteThreadCount == 0)
{
CurrentTaskFinished();
}
}
}

Expand Down

0 comments on commit 57da19b

Please sign in to comment.