以下が実行されるたびに、Mike の例外がキャッチされます。
各タスク間の継続コンテキストを含む WhenAll シーケンシャルですか、それともすべてのタスクが同時に実行されますか? 同時の場合、Mitch ではなく、Mike の例外が常にキャッチされるのはなぜですか。ミッチにチャンスを与えるために、私はマイクを遅らせた. シーケンシャルである場合、それを並行にするために何が必要ですか? Web リクエスト/ファイル処理を行うときに同時実行が適用されますか?
このコードがより深刻であると仮定すると、これは非同期に対する賢明なアプローチでしょうか? シナリオは、いくつかのメソッド (Jason、Mitch、Mike) であり、ブロックせずに同時に実行し、すべてが完了したらイベント ハンドラーを続行しますか? 例外処理の単純な実装に関して、どのような考慮事項に注意する必要がありますか? 注意すべき問題または潜在的な問題はありますか?
private async void button1_Click(object sender,EventArgs e)
{
try
{
AsyncJason c1 = new AsyncJason();
await c1.Hello();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public class AsyncJason
{
public AsyncJason()
{
}
public async Task Hello()
{
var j = await GetJasonAsync();
string[] dankeSchon = await Task.WhenAll(new Task<string>[] {GetJasonAsync(), GetMikeAsync(), GetMitchAsync()});
}
private async Task<string> GetJasonAsync()
{
var result = await Task.Run<string>(() => GetJason());
return result;
}
private string GetJason()
{
return "Jason";
}
private async Task<string> GetMitchAsync()
{
var result = await Task.Run<string>(() => GetMitch());
return result;
}
private string GetMitch()
{
throw new ArgumentException("Mitch is an idiot", "none");
}
private async Task<string> GetMikeAsync()
{
await Task.Delay(3000);
var result = await Task.Run<string>(() => GetMike());
return result;
}
private string GetMike()
{
throw new ArgumentException("Mike is an idiot", "none");
}
}