创建扩展方法时出现问题。如何使用这种扩展方法?

async Task<TResult> CancelAfterAsync<TResult>(Func<CancellationToken, Task<TResult>> startTask, TimeSpan timeout, CancellationToken cancellationToken)
{
using (var timeoutCancellation = new CancellationTokenSource())
using (var combinedCancellation = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCancellation.Token))
{
    var originalTask = startTask(combinedCancellation.Token);
    var delayTask = Task.Delay(timeout, timeoutCancellation.Token);
    var completedTask = await Task.WhenAny(originalTask, delayTask);
    // Cancel timeout to stop either task:
    // - Either the original task completed, so we need to cancel the delay task.
    // - Or the timeout expired, so we need to cancel the original task.
    // Canceling will not affect a task, that is already completed.
    timeoutCancellation.Cancel();
    if (completedTask == originalTask)
    {
        // original task completed
        return await originalTask;
    }
    else
    {
        // timeout
        throw new TimeoutException();
    }
}
}

Asynchronously wait for Task<T> to complete with timeout

我在stackoverflow的这里找到了这个异步方法,我想创建该方法的扩展方法,但是在第一行中得到了以下错误消息:

名称空间不能直接包含成员,例如字段或方法

扩展方法有什么问题?

public static async Task<TResult> CancelAfterAsync<TResult>(Func<CancellationToken, Task<TResult>> startTask, TimeSpan timeout, CancellationToken cancellationToken)
{
    using (var timeoutCancellation = new CancellationTokenSource())
    using (var combinedCancellation = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCancellation.Token))
    {
        var originalTask = startTask(combinedCancellation.Token);
        var delayTask = Task.Delay(timeout, timeoutCancellation.Token);
        var completedTask = await Task.WhenAny(originalTask, delayTask);
        // Cancel timeout to stop either task:
        // - Either the original task completed, so we need to cancel the delay task.
        // - Or the timeout expired, so we need to cancel the original task.
        // Canceling will not affect a task, that is already completed.
        timeoutCancellation.Cancel();
        if (completedTask == originalTask)
        {
            // original task completed
            return await originalTask;
        }
        else
        {
            // timeout
            throw new TimeoutException();
        }
    }
}

另外,我不知道如何在另一个类中调用这种扩展方法。

异步方法中的第一个参数CancellationToken和最后一个参数cancelToken是什么?

我想创建以下异步方法UpdatePlayerCountryData的任务,并将其与扩展方法一起使用,以了解UpdatePlayerCountryData是否在5秒钟内完成,如果没有,则抛出新的TimeoutException();。

如何将UpdatePlayerCountryData与扩展方法一起使用?如何从另一个类调用CancelAfterAsync?

private async Task UpdatePlayerCountryData(string country, string city)
{
     var resultprofile = await PlayFabClientAPI.UpdateUserDataAsync(new PlayFab.ClientModels.UpdateUserDataRequest()
     {
         Data = new Dictionary<string, string>() {
            {"Country", country},
            {"City", city}
            },
         Permission = PlayFab.ClientModels.UserDataPermission.Public
     });

     if (resultprofile.Error != null)
         Console.WriteLine(resultprofile.Error.GenerateErrorReport());
     else
     {
         Console.WriteLine("Successfully updated user data");
     }
}
评论