2

一直在尝试按顺序执行任务,但它们是按随机顺序执行的。

  • 在 .ContinueWith 之后附加 .Unwrap 没有帮助
  • 从这些方法而不是 Task 返回 T 的 Task 并将它们的结果分配给调用者也不起作用

不确定我的方法的签名,它们是否应该包含 async/await。

测序任务:

Task biographies = LoadArtistBiographies(apiKey);
Task blogs = LoadArtistBlogs(apiKey);
Task familiarity = LoadArtistFamiliarity(apiKey);
Task hottness = LoadArtistHottness(apiKey);
Task images = LoadArtistImages(apiKey);

await biographies.ContinueWith(b => blogs);
await blogs.ContinueWith(f => familiarity);
await familiarity.ContinueWith(h => hottness);
await hottness.ContinueWith(i => images);
await images;

执行方法示例:

private async Task LoadArtistBiographies(string apiKey)
{
    var parameters = new ArtistBiographiesParameters();
    parameters.SetDefaultValues();
    parameters.ApiKey = apiKey;
    parameters.Id = _artistId;
    ArtistBiographies biographies = await Queries.ArtistBiographies(parameters);
    ItemsControlBiographies.ItemsSource = biographies.Biographies;
}

Queries.* 方法也是异步的:

public static async Task<ArtistBlogs> ArtistBlogs(ArtistBlogsParameters parameters)

链接本身正在执行异步任务的任务的正确语法是什么?

4

4 回答 4

6

如果要按特定顺序执行任务,则应await直接执行:

await LoadArtistBiographies(apiKey);
await LoadArtistBlogs(apiKey);
await LoadArtistFamiliarity(apiKey);
await LoadArtistHottness(apiKey);
await LoadArtistImages(apiKey);

这将导致在LoadArtistBlogs第一个任务完成后安排第二个任务 ( )。

现在,任务正在“以随机顺序”执行,因为您已将它们分配给Task实例,这允许每个实例同时执行。

话虽如此,我实际上建议更改您的方法以返回值,而不是将它们分配给方法中的数据源:

private async Task<Biographies> LoadArtistBiographiesAsync(string apiKey)
{
    var parameters = new ArtistBiographiesParameters();
    parameters.SetDefaultValues();
    parameters.ApiKey = apiKey;
    parameters.Id = _artistId;
    var bio = await Queries.ArtistBiographies(parameters);
    return bio.Biographies;
}

然后你可以把这些写成:

ItemsControlBiographies.ItemsSource = await LoadArtistBiographiesAsync(apiKey);
// Other methods below, with await as this example

在我看来,这使得逻辑流经异步方法时的意图更加清晰。

于 2013-06-17T17:23:39.037 回答
2

您的示例代码将开始执行所有任务,而无需等待每个任务完成。然后它等待它们按顺序完成

关键是async当你调用一个方法时它就会启动。因此,如果您还不想启动它,请不要调用该方法:

await LoadArtistBiographies(apiKey);
await LoadArtistBlogs(apiKey);
await LoadArtistFamiliarity(apiKey);
await LoadArtistHottness(apiKey);
await LoadArtistImages(apiKey);
于 2013-06-17T17:23:46.240 回答
1

await将等待给定任务完成,它不会启动任务。您的 Load* 方法最有可能开始一项任务。所有五个任务都以任意顺序运行。

当您到达 时await,您的任务可能已经完成,也可能尚未完成。不要紧。你调用ContinueWith它,告诉你的任务一旦完成它应该继续使用这个方法。这将返回一个新任务,您最终会在该任务上等待。

于 2013-06-17T17:24:01.553 回答
0

实际上我刚刚找到了一种方法,但没有 ContinueWith :

ArtistBiographies biographies = await LoadArtistBiographies(apiKey);
ItemsControlBiographies.ItemsSource = biographies.Biographies;

ArtistBlogs blogs = await LoadArtistBlogs(apiKey);
ItemsControlBlogs.ItemsSource = blogs.Blogs;

ArtistFamiliarity familiarity = await LoadArtistFamiliarity(apiKey);
ContentControlFamiliarity.Content = familiarity.artist;

ArtistHotttnesss hottness = await LoadArtistHottness(apiKey);
ContentControlHottness.Content = hottness.Artist;

ArtistImages images = await LoadArtistImages(apiKey);
ItemsControlImages.ItemsSource = images.Images;

好奇是否有人可以使用 ContinueWith 提供答案。

于 2013-06-17T17:24:14.913 回答