Skip to main content
 首页 » 编程设计

c#之我应该如何在没有 C# 的异步的情况下实现这种在循环中异步调用的模式

2024年02月20日10tintown

我正在尝试在 C#4 中实现使用分页请求异步列出 AmazonS3 存储桶中所有对象的简单任务。我使用以下代码片段在 C#5 中工作:

var listRequest = new ListObjectsRequest().WithBucketName(bucketName); 
ListObjectsResponse listResponse = null; 
var list = new List<List<S3Object>>(); 
 
while (listResponse == null || listResponse.IsTruncated) 
{ 
    listResponse = await Task<ListObjectsResponse>.Factory.FromAsync( 
        client.BeginListObjects, client.EndListObjects, listRequest, null); 
 
    list.Add(listResponse.S3Objects); 
 
    if (listResponse.IsTruncated) 
    { 
        listRequest.Marker = listResponse.NextMarker; 
    } 
} 
 
return list.SelectMany(l => l); 

我正在异步调用 BeginListObjects/EndListObjects 对,但每次响应显示它被截断时我都必须重复该调用。这段代码对我有用。

但是,我现在想在 C#4 的 TPL 中执行此操作,在那里我没有使用 async/await 的奢侈,并且想了解是否可以使用延续来完成此操作。

我如何在 C#4 中做同样的事情?

请您参考如下方法:

好吧,与其将项目与每个任务/延续一起放入列表中,不如在非等待模型中让每个任务/延续返回整个序列更容易。鉴于此,我使用以下辅助方法将每个迭代结果添加到总计中。

public static Task<IEnumerable<T>> Concat<T>(Task<IEnumerable<T>> first 
        , Task<IEnumerable<T>> second) 
{ 
    return Task.Factory.ContinueWhenAll(new[] { first, second }, _ => 
    { 
        return first.Result.Concat(second.Result); 
    }); 
} 

接下来,我使用 follow 方法将单个结果的任务转换为序列任务(仅包含那一项)。

public static Task<IEnumerable<T>> ToSequence<T>(this Task<T> task) 
{ 
    var tcs = new TaskCompletionSource<IEnumerable<T>>(); 
    task.ContinueWith(_ => 
    { 
        if (task.IsCanceled) 
            tcs.SetCanceled(); 
        else if (task.IsFaulted) 
            tcs.SetException(task.Exception); 
        else 
            tcs.SetResult(Enumerable.Repeat(task.Result, 1)); 
    }); 
 
    return tcs.Task; 
} 

请注意,您有一些未定义的字段/局部变量;我假设您可以毫无困难地将它们添加到适当的方法中。

private Task<IEnumerable<S3Object>> method(object sender, EventArgs e) 
{ 
 
    ListObjectsResponse listResponse = null; 
    return Task<ListObjectsResponse>.Factory.FromAsync( 
        client.BeginListObjects, client.EndListObjects, listRequest, null) 
        .ToSequence() 
        .ContinueWith(continuation); 
} 

这里是真正的魔法发生的地方。基本上,

public Task<IEnumerable<S3Object>> continuation(Task<IEnumerable<S3Object>> task) 
{ 
    if (task.Result == null)  //not quite sure what null means here//may need to edit this recursive case 
    { 
        return Task<ListObjectsResponse>.Factory.FromAsync( 
                client.BeginListObjects, client.EndListObjects, listRequest, null) 
                .ToSequence() 
                .ContinueWith(continuation); 
    } 
    else if (task.Result.First().IsTruncated) 
    { 
        //if the results were trunctated then concat those results with  
        //TODO modify the request marker here; either create a new one or store the request as a field and mutate. 
        Task<IEnumerable<S3Object>> nextBatch = Task<ListObjectsResponse>.Factory.FromAsync( 
                client.BeginListObjects, client.EndListObjects, listRequest, null) 
                .ToSequence() 
                .ContinueWith(continuation); 
        return Concat(nextBatch, task);//recursive continuation call 
    } 
    else //if we're done it means the existing results are sufficient 
    { 
        return task; 
    } 
}