diff --git a/SteamKit2/SteamKit2/Types/JobID.cs b/SteamKit2/SteamKit2/Types/JobID.cs index 251919a8d..c8de46225 100644 --- a/SteamKit2/SteamKit2/Types/JobID.cs +++ b/SteamKit2/SteamKit2/Types/JobID.cs @@ -173,7 +173,7 @@ public sealed class AsyncJob : AsyncJob, IAsyncJob public AsyncJob( SteamClient client, JobID jobId ) : base( client, jobId ) { - tcs = new TaskCompletionSource(); + tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); } @@ -301,7 +301,7 @@ public sealed class ResultSet public AsyncJobMultiple( SteamClient client, JobID jobId, Predicate finishCondition ) : base( client, jobId ) { - tcs = new TaskCompletionSource(); + tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); this.finishCondition = finishCondition; } diff --git a/SteamKit2/Tests/AsyncJobFacts.cs b/SteamKit2/Tests/AsyncJobFacts.cs index e251d0da9..dd89eb89b 100644 --- a/SteamKit2/Tests/AsyncJobFacts.cs +++ b/SteamKit2/Tests/AsyncJobFacts.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; using SteamKit2; using Xunit; @@ -360,5 +361,51 @@ public async void AsyncJobMultipleThrowsFailureExceptionOnFailure() await Assert.ThrowsAsync( typeof( AsyncJobFailedException ), async () => await asyncTask ); } + + [Fact] + public async Task AsyncJobContinuesAsynchronously() + { + SteamClient client = new SteamClient(); + + var asyncJob = new AsyncJob(client, 123); + var asyncTask = asyncJob.ToTask(); + + var continuationThreadID = -1; + var continuation = asyncTask.ContinueWith(t => + { + continuationThreadID = Thread.CurrentThread.ManagedThreadId; + }, TaskContinuationOptions.ExecuteSynchronously); + + var completionThreadID = Thread.CurrentThread.ManagedThreadId; + asyncJob.AddResult(new Callback { JobID = 123 }); + + await continuation; + + Assert.NotEqual(-1, continuationThreadID); + Assert.NotEqual(completionThreadID, continuationThreadID); + } + + [Fact] + public async Task AsyncJobMultipleContinuesAsynchronously() + { + SteamClient client = new SteamClient(); + + var asyncJob = new AsyncJobMultiple(client, 123, call => true); + var asyncTask = asyncJob.ToTask(); + + var continuationThreadID = -1; + var continuation = asyncTask.ContinueWith(t => + { + continuationThreadID = Thread.CurrentThread.ManagedThreadId; + }, TaskContinuationOptions.ExecuteSynchronously); + + var completionThreadID = Thread.CurrentThread.ManagedThreadId; + asyncJob.AddResult(new Callback { JobID = 123 }); + + await continuation; + + Assert.NotEqual(-1, continuationThreadID); + Assert.NotEqual(completionThreadID, continuationThreadID); + } } }