Share via


方法: Task.WhenAll を使用して AsyncWalkthrough を拡張する (C# および Visual Basic)

Task.WhenAll メソッドを使うと チュートリアル: Async と Await を使用した Web へのアクセス (C# および Visual Basic) の非同期ソリューションのパフォーマンスを向上できます。 このメソッドは、タスクのコレクションとして表される、複数の非同期操作を非同期に待機します。

このチュートリアルでは、Web サイトが異なる速度でダウンロードされることに気付きます。 Web サイトの 1 つが非常に低速な場合があります。これは残りのすべてのダウンロードを遅延させます。 このチュートリアルで構築した非同期ソリューションを実行すると、プログラムを待たない場合には簡単に終了することができますが、よりよい方法は、すべてのダウンロードを同時に開始して、遅延したダウンロードを待たずに早いダウンロードが継続できるようにすることです。

タスクのコレクションに Task.WhenAll メソッドを適用します。 WhenAll を適用すると、コレクション内のすべてのタスクが完了するまで完了しない 1 つのタスクを返します。 タスクは並列で実行されるように見えますが、追加のスレッドは作成されません。 タスクは任意の順序で完了します。

重要

次の手順では チュートリアル: Async と Await を使用した Web へのアクセス (C# および Visual Basic) で開発した非同期アプリケーションの拡張機能について説明します。チュートリアルを完了するか、または開発者コード サンプル からコードをダウンロードして、アプリケーションを開発できます。

この例を実行するには、Visual Studio 2012、Visual Studio 2013、Visual Studio Express 2012 for Windows Desktop、Visual Studio Express 2013 for Windows、または .NET Framework 4.5 か 4.5.1 がコンピューターにインストールされている必要があります。

GetURLContentsAsync ソリューションに Task.WhenAll を追加するには

  1. チュートリアル: Async と Await を使用した Web へのアクセス (C# および Visual Basic) で開発された最初のアプリケーションに ProcessURLAsync メソッドを追加します。

    • コードを開発者コード サンプル からダウンロードする場合は、AsyncWalkthrough プロジェクトを開き、MainWindow.xaml.vb または MainWindow.xaml.cs ファイルに ProcessURLAsync を追加します。

    • チュートリアルを実行してコードを開発する場合は、GetURLContentsAsync メソッドを含むアプリケーションに ProcessURLAsync を追加します。 このアプリケーションの MainWindow.xaml.vb または MainWindow.xaml.cs ファイルは、「チュートリアルの完全なコード例」のセクションの最初の例です。

    ProcessURLAsync メソッドは、元のチュートリアルの SumPageSizesAsync の For Each または foreach ループの本体のアクションを統合します。 このメソッドは、指定した Web サイトのコンテンツをバイト配列として非同期的にダウンロードし、バイト配列の長さを表示して返します。

    Private Async Function ProcessURLAsync(url As String) As Task(Of Integer)
    
        Dim byteArray = Await GetURLContentsAsync(url)
        DisplayResults(url, byteArray)
        Return byteArray.Length
    End Function
    
    private async Task<int> ProcessURLAsync(string url)
    {
        var byteArray = await GetURLContentsAsync(url);
        DisplayResults(url, byteArray);
        return byteArray.Length;
    }
    
  2. 次のコードに示すように、SumPageSizesAsync の For Each または foreach のループをコメント アウトするか削除します。

    'Dim total = 0 
    'For Each url In urlList 
    
    '    Dim urlContents As Byte() = Await GetURLContentsAsync(url) 
    
    '    ' The previous line abbreviates the following two assignment statements. 
    
    '    ' GetURLContentsAsync returns a task. At completion, the task 
    '    ' produces a byte array. 
    '    'Dim getContentsTask As Task(Of Byte()) = GetURLContentsAsync(url) 
    '    'Dim urlContents As Byte() = Await getContentsTask 
    
    '    DisplayResults(url, urlContents) 
    
    '    ' Update the total. 
    '    total += urlContents.Length 
    'Next
    
    //var total = 0; 
    //foreach (var url in urlList) 
    //{ 
    //    byte[] urlContents = await GetURLContentsAsync(url); 
    
    //    // The previous line abbreviates the following two assignment statements. 
    //    // GetURLContentsAsync returns a Task<T>. At completion, the task 
    //    // produces a byte array. 
    //    //Task<byte[]> getContentsTask = GetURLContentsAsync(url); 
    //    //byte[] urlContents = await getContentsTask; 
    
    //    DisplayResults(url, urlContents); 
    
    //    // Update the total.           
    //    total += urlContents.Length; 
    //}
    
  3. タスクのコレクションを作成します。 次のコードは、ToArray``1 メソッドによって実行されるときに、各 Web サイトのコンテンツをダウンロードするタスクのコレクションを作成するクエリを定義します。 タスクは、クエリが評価されるときに開始されます。

    urlList の宣言の後の SumPageSizesAsync メソッドに、次のコードを追加します。

    ' Create a query.  
    Dim downloadTasksQuery As IEnumerable(Of Task(Of Integer)) =
        From url In urlList Select ProcessURLAsync(url)
    
    ' Use ToArray to execute the query and start the download tasks. 
    Dim downloadTasks As Task(Of Integer)() = downloadTasksQuery.ToArray()
    
    // Create a query. 
    IEnumerable<Task<int>> downloadTasksQuery = 
        from url in urlList select ProcessURLAsync(url);
    
    // Use ToArray to execute the query and start the download tasks.
    Task<int>[] downloadTasks = downloadTasksQuery.ToArray();
    
  4. Task.WhenAll をタスクのコレクション downloadTasks に適用します。 Task.WhenAll は、タスクのコレクションのすべてのタスクが完了すると完了する、単一のタスクを返します。

    次の例では、Await または await 式は WhenAll が返す 1 つのタスクの完了を待機します。 式は、各整数がダウンロードされたサイトの長さである、整数の配列に評価します。 SumPageSizesAsync の、前の手順で追加したコードの直後に、次のコードを追加します。

    ' Await the completion of all the running tasks. 
    Dim lengths As Integer() = Await Task.WhenAll(downloadTasks)
    
    '' The previous line is equivalent to the following two statements. 
    'Dim whenAllTask As Task(Of Integer()) = Task.WhenAll(downloadTasks) 
    'Dim lengths As Integer() = Await whenAllTask
    
    // Await the completion of all the running tasks. 
    int[] lengths = await Task.WhenAll(downloadTasks);
    
    //// The previous line is equivalent to the following two statements. 
    //Task<int[]> whenAllTask = Task.WhenAll(downloadTasks); 
    //int[] lengths = await whenAllTask;
    
  5. 最後に Sum メソッドを使って、すべての Web サイトの長さの合計を計算します。 SumPageSizesAsync に次の行を追加します。

    Dim total = lengths.Sum()
    
    int total = lengths.Sum();
    

HttpClient.GetByteArrayAsync ソリューションに Task.WhenAll を追加するには

  1. チュートリアル: Async と Await を使用した Web へのアクセス (C# および Visual Basic) で開発される 2 番目のアプリケーションに、ProcessURLAsync の次のバージョンを追加します。

    • コードを開発者コード サンプル からダウンロードする場合は、AsyncWalkthrough_HttpClient プロジェクトを開き、MainWindow.xaml.vb または MainWindow.xaml.cs ファイルに ProcessURLAsync を追加します。

    • チュートリアルを実行してコードを開発する場合は、HttpClient.GetByteArrayAsync メソッドを使うアプリケーションに ProcessURLAsync を追加します。 このアプリケーションの MainWindow.xaml.vb または MainWindow.xaml.cs ファイルは、「チュートリアルの完全なコード例」のセクションの 2 番目の例です。

    ProcessURLAsync メソッドは、元のチュートリアルの SumPageSizesAsync の For Each または foreach ループの本体のアクションを統合します。 このメソッドは、指定した Web サイトのコンテンツをバイト配列として非同期的にダウンロードし、バイト配列の長さを表示して返します。

    前の手順の ProcessURLAsync メソッドとの唯一の違いは、HttpClient インスタンス client の使用です。

    Private Async Function ProcessURLAsync(url As String, client As HttpClient) As Task(Of Integer)
    
        Dim byteArray = Await client.GetByteArrayAsync(url)
        DisplayResults(url, byteArray)
        Return byteArray.Length
    End Function
    
    async Task<int> ProcessURL(string url, HttpClient client)
    {
        byte[] byteArray = await client.GetByteArrayAsync(url);
        DisplayResults(url, byteArray);
        return byteArray.Length;
    }
    
  2. 次のコードに示すように、SumPageSizesAsync の For Each または foreach のループをコメント アウトするか削除します。

    'Dim total = 0 
    'For Each url In urlList 
    '    ' GetByteArrayAsync returns a task. At completion, the task 
    '    ' produces a byte array. 
    '    Dim urlContents As Byte() = Await client.GetByteArrayAsync(url) 
    
    '    ' The following two lines can replace the previous assignment statement. 
    '    'Dim getContentsTask As Task(Of Byte()) = client.GetByteArrayAsync(url) 
    '    'Dim urlContents As Byte() = Await getContentsTask 
    
    '    DisplayResults(url, urlContents) 
    
    '    ' Update the total. 
    '    total += urlContents.Length 
    'Next
    
    //var total = 0; 
    //foreach (var url in urlList) 
    //{ 
    //    // GetByteArrayAsync returns a Task<T>. At completion, the task 
    //    // produces a byte array. 
    //    byte[] urlContent = await client.GetByteArrayAsync(url); 
    
    //    // The previous line abbreviates the following two assignment 
    //    // statements. 
    //    Task<byte[]> getContentTask = client.GetByteArrayAsync(url); 
    //    byte[] urlContent = await getContentTask; 
    
    //    DisplayResults(url, urlContent); 
    
    //    // Update the total. 
    //    total += urlContent.Length; 
    //}
    
  3. ToArray``1 メソッドによって実行されるときに、各 Web サイトのコンテンツをダウンロードするタスクのコレクションを作成するクエリを定義します。 タスクは、クエリが評価されるときに開始されます。

    client および urlList の宣言の後の SumPageSizesAsync メソッドに、次のコードを追加します。

    ' Create a query. 
    Dim downloadTasksQuery As IEnumerable(Of Task(Of Integer)) =
        From url In urlList Select ProcessURLAsync(url, client)
    
    ' Use ToArray to execute the query and start the download tasks. 
    Dim downloadTasks As Task(Of Integer)() = downloadTasksQuery.ToArray()
    
    // Create a query.
    IEnumerable<Task<int>> downloadTasksQuery = 
        from url in urlList select ProcessURL(url, client);
    
    // Use ToArray to execute the query and start the download tasks.
    Task<int>[] downloadTasks = downloadTasksQuery.ToArray();
    
  4. 次に、Task.WhenAll をタスクのコレクション downloadTasks に適用します。 Task.WhenAll は、タスクのコレクションのすべてのタスクが完了すると完了する、単一のタスクを返します。

    次の例では、Await または await 式は WhenAll が返す 1 つのタスクの完了を待機します。 完了すると、Await または await 式は、各整数がダウンロードされたサイトの長さである、整数の配列に評価します。 SumPageSizesAsync の、前の手順で追加したコードの直後に、次のコードを追加します。

    ' Await the completion of all the running tasks. 
    Dim lengths As Integer() = Await Task.WhenAll(downloadTasks)
    
    '' The previous line is equivalent to the following two statements. 
    'Dim whenAllTask As Task(Of Integer()) = Task.WhenAll(downloadTasks) 
    'Dim lengths As Integer() = Await whenAllTask
    
    // Await the completion of all the running tasks. 
    int[] lengths = await Task.WhenAll(downloadTasks);
    
    //// The previous line is equivalent to the following two statements. 
    //Task<int[]> whenAllTask = Task.WhenAll(downloadTasks); 
    //int[] lengths = await whenAllTask;
    
  5. 最後に Sum メソッドを使って、すべての Web サイトの長さの合計を計算します。 SumPageSizesAsync に次の行を追加します。

    Dim total = lengths.Sum()
    
    int total = lengths.Sum();
    

Task.WhenAll ソリューションをテストするには

使用例

次のコードは、GetURLContentsAsync メソッドを使用して Web からコンテンツをダウンロードするプロジェクトの拡張機能を示します。

' Add the following Imports statements, and add a reference for System.Net.Http. 
Imports System.Net.Http
Imports System.Net
Imports System.IO


Class MainWindow

    Async Sub startButton_Click(sender As Object, e As RoutedEventArgs) Handles startButton.Click

        resultsTextBox.Clear()

        ' One-step async call.
        Await SumPageSizesAsync()

        '' Two-step async call. 
        'Dim sumTask As Task = SumPageSizesAsync() 
        'Await sumTask

        resultsTextBox.Text &= vbCrLf & "Control returned to button1_Click." 
    End Sub 


    Private Async Function SumPageSizesAsync() As Task

        ' Make a list of web addresses. 
        Dim urlList As List(Of String) = SetUpURLList()

        ' Create a query.  
        Dim downloadTasksQuery As IEnumerable(Of Task(Of Integer)) =
            From url In urlList Select ProcessURLAsync(url)

        ' Use ToArray to execute the query and start the download tasks. 
        Dim downloadTasks As Task(Of Integer)() = downloadTasksQuery.ToArray()

        ' You can do other work here before awaiting. 

        ' Await the completion of all the running tasks. 
        Dim lengths As Integer() = Await Task.WhenAll(downloadTasks)

        '' The previous line is equivalent to the following two statements. 
        'Dim whenAllTask As Task(Of Integer()) = Task.WhenAll(downloadTasks) 
        'Dim lengths As Integer() = Await whenAllTask 

        Dim total = lengths.Sum()

        'Dim total = 0 
        'For Each url In urlList 

        '    Dim urlContents As Byte() = Await GetURLContentsAsync(url) 

        '    ' The previous line abbreviates the following two assignment statements. 

        '    ' GetURLContentsAsync returns a task. At completion, the task 
        '    ' produces a byte array. 
        '    'Dim getContentsTask As Task(Of Byte()) = GetURLContentsAsync(url) 
        '    'Dim urlContents As Byte() = Await getContentsTask 

        '    DisplayResults(url, urlContents) 

        '    ' Update the total. 
        '    total += urlContents.Length 
        'Next 

        ' Display the total count for all of the web addresses.
        resultsTextBox.Text &= String.Format(vbCrLf & vbCrLf &
                                             "Total bytes returned:  {0}" & vbCrLf, total)
    End Function 


    Private Function SetUpURLList() As List(Of String)

        Dim urls = New List(Of String) From
            {
                "https://msdn.microsoft.com",
                "https://msdn.microsoft.com/en-us/library/hh290136.aspx",
                "https://msdn.microsoft.com/en-us/library/ee256749.aspx",
                "https://msdn.microsoft.com/en-us/library/hh290138.aspx",
                "https://msdn.microsoft.com/en-us/library/hh290140.aspx",
                "https://msdn.microsoft.com/en-us/library/dd470362.aspx",
                "https://msdn.microsoft.com/en-us/library/aa578028.aspx",
                "https://msdn.microsoft.com/en-us/library/ms404677.aspx",
                "https://msdn.microsoft.com/en-us/library/ff730837.aspx"
            }
        Return urls
    End Function 


    ' The actions from the foreach loop are moved to this async method. 
    Private Async Function ProcessURLAsync(url As String) As Task(Of Integer)

        Dim byteArray = Await GetURLContentsAsync(url)
        DisplayResults(url, byteArray)
        Return byteArray.Length
    End Function 


    Private Async Function GetURLContentsAsync(url As String) As Task(Of Byte())

        ' The downloaded resource ends up in the variable named content. 
        Dim content = New MemoryStream()

        ' Initialize an HttpWebRequest for the current URL. 
        Dim webReq = CType(WebRequest.Create(url), HttpWebRequest)

        ' Send the request to the Internet resource and wait for 
        ' the response. 
        Using response As WebResponse = Await webReq.GetResponseAsync()
            ' Get the data stream that is associated with the specified URL. 
            Using responseStream As Stream = response.GetResponseStream()
                ' Read the bytes in responseStream and copy them to content.   
                ' CopyToAsync returns a Task, not a Task<T>.
                Await responseStream.CopyToAsync(content)
            End Using 
        End Using 

        ' Return the result as a byte array. 
        Return content.ToArray()
    End Function 



    Private Sub DisplayResults(url As String, content As Byte())

        ' Display the length of each website. The string format  
        ' is designed to be used with a monospaced font, such as 
        ' Lucida Console or Global Monospace. 
        Dim bytes = content.Length
        ' Strip off the "http://". 
        Dim displayURL = url.Replace("http://", "")
        resultsTextBox.Text &= String.Format(vbCrLf & "{0,-58} {1,8}", displayURL, bytes)
    End Sub 

End Class
// Add the following using directives, and add a reference for System.Net.Http. 
using System.Net.Http;
using System.IO;
using System.Net;

namespace AsyncExampleWPF_WhenAll
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


        private async void startButton_Click(object sender, RoutedEventArgs e)
        {
            resultsTextBox.Clear();

            // Two-step async call.
            Task sumTask = SumPageSizesAsync();
            await sumTask;

            // One-step async call. 
            //await SumPageSizesAsync();

            resultsTextBox.Text += "\r\nControl returned to startButton_Click.\r\n";
        }


        private async Task SumPageSizesAsync()
        {
            // Make a list of web addresses.
            List<string> urlList = SetUpURLList();

            // Create a query. 
            IEnumerable<Task<int>> downloadTasksQuery = 
                from url in urlList select ProcessURLAsync(url);

            // Use ToArray to execute the query and start the download tasks.
            Task<int>[] downloadTasks = downloadTasksQuery.ToArray();

            // You can do other work here before awaiting. 

            // Await the completion of all the running tasks. 
            int[] lengths = await Task.WhenAll(downloadTasks);

            //// The previous line is equivalent to the following two statements. 
            //Task<int[]> whenAllTask = Task.WhenAll(downloadTasks); 
            //int[] lengths = await whenAllTask; 

            int total = lengths.Sum();

            //var total = 0; 
            //foreach (var url in urlList) 
            //{ 
            //    byte[] urlContents = await GetURLContentsAsync(url); 

            //    // The previous line abbreviates the following two assignment statements. 
            //    // GetURLContentsAsync returns a Task<T>. At completion, the task 
            //    // produces a byte array. 
            //    //Task<byte[]> getContentsTask = GetURLContentsAsync(url); 
            //    //byte[] urlContents = await getContentsTask; 

            //    DisplayResults(url, urlContents); 

            //    // Update the total.           
            //    total += urlContents.Length; 
            //} 

            // Display the total count for all of the websites.
            resultsTextBox.Text +=
                string.Format("\r\n\r\nTotal bytes returned:  {0}\r\n", total);
        }


        private List<string> SetUpURLList()
        {
            List<string> urls = new List<string> 
            { 
                "https://msdn.microsoft.com",
                "https://msdn.microsoft.com/library/windows/apps/br211380.aspx",
                "https://msdn.microsoft.com/en-us/library/hh290136.aspx",
                "https://msdn.microsoft.com/en-us/library/ee256749.aspx",
                "https://msdn.microsoft.com/en-us/library/hh290138.aspx",
                "https://msdn.microsoft.com/en-us/library/hh290140.aspx",
                "https://msdn.microsoft.com/en-us/library/dd470362.aspx",
                "https://msdn.microsoft.com/en-us/library/aa578028.aspx",
                "https://msdn.microsoft.com/en-us/library/ms404677.aspx",
                "https://msdn.microsoft.com/en-us/library/ff730837.aspx"
            };
            return urls;
        }



        // The actions from the foreach loop are moved to this async method. 
        private async Task<int> ProcessURLAsync(string url)
        {
            var byteArray = await GetURLContentsAsync(url);
            DisplayResults(url, byteArray);
            return byteArray.Length;
        }


        private async Task<byte[]> GetURLContentsAsync(string url)
        {
            // The downloaded resource ends up in the variable named content. 
            var content = new MemoryStream();

            // Initialize an HttpWebRequest for the current URL. 
            var webReq = (HttpWebRequest)WebRequest.Create(url);

            // Send the request to the Internet resource and wait for 
            // the response. 
            using (WebResponse response = await webReq.GetResponseAsync())
            {
                // Get the data stream that is associated with the specified url. 
                using (Stream responseStream = response.GetResponseStream())
                {
                    await responseStream.CopyToAsync(content);
                }
            }

            // Return the result as a byte array. 
            return content.ToArray();

        }


        private void DisplayResults(string url, byte[] content)
        {
            // Display the length of each website. The string format  
            // is designed to be used with a monospaced font, such as 
            // Lucida Console or Global Monospace. 
            var bytes = content.Length;
            // Strip off the "http://".
            var displayURL = url.Replace("http://", "");
            resultsTextBox.Text += string.Format("\n{0,-58} {1,8}", displayURL, bytes);

        }
    }
}

次のコードは、HttpClient.GetByteArrayAsync メソッドを使用して Web からコンテンツをダウンロードするプロジェクトの拡張機能を示します。

' Add the following Imports statements, and add a reference for System.Net.Http. 
Imports System.Net.Http
Imports System.Net
Imports System.IO

Class MainWindow

    Async Sub startButton_Click(sender As Object, e As RoutedEventArgs) Handles startButton.Click

        resultsTextBox.Clear()

        '' One-step async call.
        Await SumPageSizesAsync()

        '' Two-step async call. 
        'Dim sumTask As Task = SumPageSizesAsync() 
        'Await sumTask

        resultsTextBox.Text &= vbCrLf & "Control returned to button1_Click." 
    End Sub 


    Private Async Function SumPageSizesAsync() As Task

        ' Declare an HttpClient object and increase the buffer size. The 
        ' default buffer size is 65,536. 
        Dim client As HttpClient =
            New HttpClient() With {.MaxResponseContentBufferSize = 1000000}

        ' Make a list of web addresses. 
        Dim urlList As List(Of String) = SetUpURLList()

        ' Create a query. 
        Dim downloadTasksQuery As IEnumerable(Of Task(Of Integer)) =
            From url In urlList Select ProcessURLAsync(url, client)

        ' Use ToArray to execute the query and start the download tasks. 
        Dim downloadTasks As Task(Of Integer)() = downloadTasksQuery.ToArray()

        ' You can do other work here before awaiting. 

        ' Await the completion of all the running tasks. 
        Dim lengths As Integer() = Await Task.WhenAll(downloadTasks)

        '' The previous line is equivalent to the following two statements. 
        'Dim whenAllTask As Task(Of Integer()) = Task.WhenAll(downloadTasks) 
        'Dim lengths As Integer() = Await whenAllTask 

        Dim total = lengths.Sum()

        'Dim total = 0 
        'For Each url In urlList 
        '    ' GetByteArrayAsync returns a task. At completion, the task 
        '    ' produces a byte array. 
        '    Dim urlContents As Byte() = Await client.GetByteArrayAsync(url) 

        '    ' The following two lines can replace the previous assignment statement. 
        '    'Dim getContentsTask As Task(Of Byte()) = client.GetByteArrayAsync(url) 
        '    'Dim urlContents As Byte() = Await getContentsTask 

        '    DisplayResults(url, urlContents) 

        '    ' Update the total. 
        '    total += urlContents.Length 
        'Next 

        ' Display the total count for all of the web addresses.
        resultsTextBox.Text &= String.Format(vbCrLf & vbCrLf &
                                             "Total bytes returned:  {0}" & vbCrLf, total)
    End Function 


    Private Function SetUpURLList() As List(Of String)

        Dim urls = New List(Of String) From
            {
                "http://www.msdn.com",
                "https://msdn.microsoft.com/en-us/library/hh290136.aspx",
                "https://msdn.microsoft.com/en-us/library/ee256749.aspx",
                "https://msdn.microsoft.com/en-us/library/hh290138.aspx",
                "https://msdn.microsoft.com/en-us/library/hh290140.aspx",
                "https://msdn.microsoft.com/en-us/library/dd470362.aspx",
                "https://msdn.microsoft.com/en-us/library/aa578028.aspx",
                "https://msdn.microsoft.com/en-us/library/ms404677.aspx",
                "https://msdn.microsoft.com/en-us/library/ff730837.aspx"
            }
        Return urls
    End Function 

    Private Async Function ProcessURLAsync(url As String, client As HttpClient) As Task(Of Integer)

        Dim byteArray = Await client.GetByteArrayAsync(url)
        DisplayResults(url, byteArray)
        Return byteArray.Length
    End Function 


    Private Sub DisplayResults(url As String, content As Byte())

        ' Display the length of each website. The string format  
        ' is designed to be used with a monospaced font, such as 
        ' Lucida Console or Global Monospace. 
        Dim bytes = content.Length
        ' Strip off the "http://". 
        Dim displayURL = url.Replace("http://", "")
        resultsTextBox.Text &= String.Format(vbCrLf & "{0,-58} {1,8}", displayURL, bytes)
    End Sub 

End Class
// Add the following using directives, and add a reference for System.Net.Http. 
using System.Net.Http;
using System.IO;
using System.Net;

namespace AsyncExampleWPF_HttpClient_WhenAll
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private async void startButton_Click(object sender, RoutedEventArgs e)
        {
            resultsTextBox.Clear();

            // One-step async call.
            await SumPageSizesAsync();

            // Two-step async call. 
            //Task sumTask = SumPageSizesAsync(); 
            //await sumTask;

            resultsTextBox.Text += "\r\nControl returned to startButton_Click.\r\n";
        }


        private async Task SumPageSizesAsync()
        {
            // Make a list of web addresses.
            List<string> urlList = SetUpURLList();


            // Declare an HttpClient object and increase the buffer size. The 
            // default buffer size is 65,536.
            HttpClient client = new HttpClient() { MaxResponseContentBufferSize = 1000000 };

            // Create a query.
            IEnumerable<Task<int>> downloadTasksQuery = 
                from url in urlList select ProcessURL(url, client);

            // Use ToArray to execute the query and start the download tasks.
            Task<int>[] downloadTasks = downloadTasksQuery.ToArray();

            // You can do other work here before awaiting. 

            // Await the completion of all the running tasks. 
            int[] lengths = await Task.WhenAll(downloadTasks);

            //// The previous line is equivalent to the following two statements. 
            //Task<int[]> whenAllTask = Task.WhenAll(downloadTasks); 
            //int[] lengths = await whenAllTask; 

            int total = lengths.Sum();

            //var total = 0; 
            //foreach (var url in urlList) 
            //{ 
            //    // GetByteArrayAsync returns a Task<T>. At completion, the task 
            //    // produces a byte array. 
            //    byte[] urlContent = await client.GetByteArrayAsync(url); 

            //    // The previous line abbreviates the following two assignment 
            //    // statements. 
            //    Task<byte[]> getContentTask = client.GetByteArrayAsync(url); 
            //    byte[] urlContent = await getContentTask; 

            //    DisplayResults(url, urlContent); 

            //    // Update the total. 
            //    total += urlContent.Length; 
            //} 

            // Display the total count for all of the web addresses.
            resultsTextBox.Text +=
                string.Format("\r\n\r\nTotal bytes returned:  {0}\r\n", total);
        }


        private List<string> SetUpURLList()
        {
            List<string> urls = new List<string> 
            { 
                "https://msdn.microsoft.com",
                "https://msdn.microsoft.com/en-us/library/hh290136.aspx",
                "https://msdn.microsoft.com/en-us/library/ee256749.aspx",
                "https://msdn.microsoft.com/en-us/library/hh290138.aspx",
                "https://msdn.microsoft.com/en-us/library/hh290140.aspx",
                "https://msdn.microsoft.com/en-us/library/dd470362.aspx",
                "https://msdn.microsoft.com/en-us/library/aa578028.aspx",
                "https://msdn.microsoft.com/en-us/library/ms404677.aspx",
                "https://msdn.microsoft.com/en-us/library/ff730837.aspx"
            };
            return urls;
        }


        // The actions from the foreach loop are moved to this async method.
        async Task<int> ProcessURL(string url, HttpClient client)
        {
            byte[] byteArray = await client.GetByteArrayAsync(url);
            DisplayResults(url, byteArray);
            return byteArray.Length;
        }


        private void DisplayResults(string url, byte[] content)
        {
            // Display the length of each web site. The string format  
            // is designed to be used with a monospaced font, such as 
            // Lucida Console or Global Monospace. 
            var bytes = content.Length;
            // Strip off the "http://".
            var displayURL = url.Replace("http://", "");
            resultsTextBox.Text += string.Format("\n{0,-58} {1,8}", displayURL, bytes);
        }
    }
}

参照

処理手順

チュートリアル: Async と Await を使用した Web へのアクセス (C# および Visual Basic)

関連項目

Task.WhenAll