mirror of
https://github.com/ppy/osu.git
synced 2025-02-15 14:42:56 +08:00
Proposal to use extension method instead of TaskChain class
This commit is contained in:
parent
248989b3eb
commit
fcfb0d52c2
@ -1,6 +1,7 @@
|
||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using NUnit.Framework;
|
||||
@ -12,15 +13,17 @@ namespace osu.Game.Tests.NonVisual
|
||||
[TestFixture]
|
||||
public class TaskChainTest
|
||||
{
|
||||
private TaskChain taskChain;
|
||||
private Task taskChain;
|
||||
|
||||
private int currentTask;
|
||||
private CancellationTokenSource globalCancellationToken;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
taskChain = Task.CompletedTask;
|
||||
|
||||
globalCancellationToken = new CancellationTokenSource();
|
||||
taskChain = new TaskChain();
|
||||
currentTask = 0;
|
||||
}
|
||||
|
||||
@ -79,9 +82,15 @@ namespace osu.Game.Tests.NonVisual
|
||||
{
|
||||
var mutex = new ManualResetEventSlim(false);
|
||||
|
||||
var task = taskChain.Add(async () =>
|
||||
var task = taskChain.ContinueWithSequential(async () =>
|
||||
{
|
||||
await Task.Run(() => mutex.Wait(globalCancellationToken.Token)).CatchUnobservedExceptions();
|
||||
try
|
||||
{
|
||||
await Task.Run(() => mutex.Wait(globalCancellationToken.Token));
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
}
|
||||
});
|
||||
|
||||
// Allow task to potentially complete
|
||||
@ -103,7 +112,7 @@ namespace osu.Game.Tests.NonVisual
|
||||
var cancellationSource = new CancellationTokenSource();
|
||||
var token = CancellationTokenSource.CreateLinkedTokenSource(cancellationSource.Token, globalCancellationToken.Token);
|
||||
|
||||
taskChain.Add(() =>
|
||||
taskChain = taskChain.ContinueWithSequential(() =>
|
||||
{
|
||||
mutex.Wait(globalCancellationToken.Token);
|
||||
completionSource.SetResult(Interlocked.Increment(ref currentTask));
|
||||
|
@ -4,6 +4,7 @@
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using osu.Framework.Extensions.ExceptionExtensions;
|
||||
using osu.Framework.Logging;
|
||||
@ -32,5 +33,43 @@ namespace osu.Game.Extensions
|
||||
Logger.Log($"Error running task: {exception}", LoggingTarget.Runtime, LogLevel.Debug);
|
||||
}, TaskContinuationOptions.NotOnRanToCompletion);
|
||||
}
|
||||
|
||||
public static Task ContinueWithSequential(this Task task, Action continuationFunction, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return task.ContinueWithSequential(() => Task.Run(continuationFunction, cancellationToken), cancellationToken);
|
||||
}
|
||||
|
||||
public static Task ContinueWithSequential(this Task task, Func<Task> continuationFunction, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<bool>();
|
||||
|
||||
task.ContinueWith(t =>
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
tcs.SetCanceled();
|
||||
}
|
||||
else
|
||||
{
|
||||
continuationFunction().ContinueWith(t2 =>
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested || t2.IsCanceled)
|
||||
{
|
||||
tcs.TrySetCanceled();
|
||||
}
|
||||
else if (t2.IsFaulted)
|
||||
{
|
||||
tcs.TrySetException(t2.Exception);
|
||||
}
|
||||
else
|
||||
{
|
||||
tcs.TrySetResult(true);
|
||||
}
|
||||
}, cancellationToken: default);
|
||||
}
|
||||
}, cancellationToken: default);
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user