You might have noticed in the walkthrough that the websites download at different rates. Sometimes one of the websites is very slow, which delays all the remaining downloads. When you run the asynchronous solutions that you build in the walkthrough, you can end the program easily if you don't want to wait, but a better option would be to start all the downloads at the same time and let faster downloads continue without waiting for the one that’s delayed.
You apply the Task.WhenAll method to a collection of tasks. The application of WhenAll returns a single task that isn’t complete until every task in the collection is completed. The tasks appear to run in parallel, but no additional threads are created. The tasks can complete in any order.
If you downloaded the code from Developer Code Samples, open the AsyncWalkthrough project, and then add ProcessURLAsync to the MainWindow.xaml.vb file.
If you developed the code by completing the walkthrough, add ProcessURLAsync to the application that includes the GetURLContentsAsync method. The MainWindow.xaml.vb file for this application is the first example in the "Complete Code Examples from the Walkthrough" section.
The ProcessURLAsync method consolidates the actions in the body of the For Each loop in SumPageSizesAsync in the original walkthrough. The method asynchronously downloads the contents of a specified website as a byte array, and then displays and returns the length of the byte array.
VB
PrivateAsyncFunction ProcessURLAsync(url AsString) As Task(OfInteger)
Dim byteArray = Await GetURLContentsAsync(url)
DisplayResults(url, byteArray)
Return byteArray.Length
EndFunction
Comment out or delete the For Each loop in SumPageSizesAsync, as the following code shows.
VB
'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
Create a collection of tasks. The following code defines a query that, when executed by the ToArray method, creates a collection of tasks that download the contents of each website. The tasks are started when the query is evaluated.
Add the following code to method SumPageSizesAsync after the declaration of urlList.
VB
' Create a query.Dim downloadTasksQuery As IEnumerable(Of Task(OfInteger)) =
From url In urlList Select ProcessURLAsync(url)
' Use ToArray to execute the query and start the download tasks.Dim downloadTasks As Task(OfInteger)() = downloadTasksQuery.ToArray()
Apply Task.WhenAll to the collection of tasks, downloadTasks. Task.WhenAll returns a single task that finishes when all the tasks in the collection of tasks have completed.
In the following example, the Await expression awaits the completion of the single task that WhenAll returns. The expression evaluates to an array of integers, where each integer is the length of a downloaded website. Add the following code to SumPageSizesAsync, just after the code that you added in the previous step.
VB
' Await the completion of all the running tasks.Dim lengths AsInteger() = 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
Finally, use the Sum method to calculate the sum of the lengths of all the websites. Add the following line to SumPageSizesAsync.
VB
Dim total = lengths.Sum()
To add Task.WhenAll to the HttpClient.GetByteArrayAsync solution
If you downloaded the code from Developer Code Samples, open the AsyncWalkthrough_HttpClient project, and then add ProcessURLAsync to the MainWindow.xaml.vb file.
If you developed the code by completing the walkthrough, add ProcessURLAsync to the application that uses the HttpClient.GetByteArrayAsync method. The MainWindow.xaml.vb file for this application is the second example in the "Complete Code Examples from the Walkthrough" section.
The ProcessURLAsync method consolidates the actions in the body of the For Each loop in SumPageSizesAsync in the original walkthrough. The method asynchronously downloads the contents of a specified website as a byte array, and then displays and returns the length of the byte array.
The only difference from the ProcessURLAsync method in the previous procedure is the use of the HttpClient instance, client.
VB
PrivateAsyncFunction ProcessURLAsync(url AsString, client As HttpClient) As Task(OfInteger)
Dim byteArray = Await client.GetByteArrayAsync(url)
DisplayResults(url, byteArray)
Return byteArray.Length
EndFunction
Comment out or delete the For Each loop in SumPageSizesAsync, as the following code shows.
VB
'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
Define a query that, when executed by the ToArray method, creates a collection of tasks that download the contents of each website. The tasks are started when the query is evaluated.
Add the following code to method SumPageSizesAsync after the declaration of client and urlList.
VB
' Create a query.Dim downloadTasksQuery As IEnumerable(Of Task(OfInteger)) =
From url In urlList Select ProcessURLAsync(url, client)
' Use ToArray to execute the query and start the download tasks.Dim downloadTasks As Task(OfInteger)() = downloadTasksQuery.ToArray()
Next, apply Task.WhenAll to the collection of tasks, downloadTasks. Task.WhenAll returns a single task that finishes when all the tasks in the collection of tasks have completed.
In the following example, the Await expression awaits the completion of the single task that WhenAll returns. When complete, the Await expression evaluates to an array of integers, where each integer is the length of a downloaded website. Add the following code to SumPageSizesAsync, just after the code that you added in the previous step.
VB
' Await the completion of all the running tasks.Dim lengths AsInteger() = 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
Finally, use the Sum method to get the sum of the lengths of all the websites. Add the following line to SumPageSizesAsync.
VB
Dim total = lengths.Sum()
To test the Task.WhenAll solutions
For either solution, choose the F5 key to run the program, and then choose the Start button. The output should resemble the output from the async solutions in Walkthrough: Accessing the Web by Using Async and Await (Visual Basic). However, notice that the websites appear in a different order each time.
Example 1
The following code shows the extensions to the project that uses the GetURLContentsAsync method to download content from the web.
VB
' 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
AsyncSub startButton_Click(sender AsObject, 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."EndSubPrivateAsyncFunction SumPageSizesAsync() As Task
' Make a list of web addresses.Dim urlList As List(OfString) = SetUpURLList()
' Create a query.Dim downloadTasksQuery As IEnumerable(Of Task(OfInteger)) =
From url In urlList Select ProcessURLAsync(url)
' Use ToArray to execute the query and start the download tasks.Dim downloadTasks As Task(OfInteger)() = downloadTasksQuery.ToArray()
' You can do other work here before awaiting.' Await the completion of all the running tasks.Dim lengths AsInteger() = 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 whenAllTaskDim 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'NextNext' Display the total count for all of the web addresses.
resultsTextBox.Text &= String.Format(vbCrLf & vbCrLf &
"Total bytes returned: {0}" & vbCrLf, total)
EndFunctionPrivateFunction SetUpURLList() As List(OfString)
Dim urls = New List(OfString) From
{
"https://msdn.microsoft.com",
"https://msdn.microsoft.com/library/hh290136.aspx",
"https://msdn.microsoft.com/library/ee256749.aspx",
"https://msdn.microsoft.com/library/hh290138.aspx",
"https://msdn.microsoft.com/library/hh290140.aspx",
"https://msdn.microsoft.com/library/dd470362.aspx",
"https://msdn.microsoft.com/library/aa578028.aspx",
"https://msdn.microsoft.com/library/ms404677.aspx",
"https://msdn.microsoft.com/library/ff730837.aspx"
}
Return urls
EndFunction' The actions from the foreach loop are moved to this async method.PrivateAsyncFunction ProcessURLAsync(url AsString) As Task(OfInteger)
Dim byteArray = Await GetURLContentsAsync(url)
DisplayResults(url, byteArray)
Return byteArray.Length
EndFunctionPrivateAsyncFunction GetURLContentsAsync(url AsString) As Task(OfByte())
' 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)
EndUsingEndUsing' Return the result as a byte array.Return content.ToArray()
EndFunctionPrivateSub DisplayResults(url AsString, content AsByte())
' 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 "https://".Dim displayURL = url.Replace("https://", "")
resultsTextBox.Text &= String.Format(vbCrLf & "{0,-58} {1,8}", displayURL, bytes)
EndSubEndClass
Example 2
The following code shows the extensions to the project that uses method HttpClient.GetByteArrayAsync to download content from the web.
VB
' 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
AsyncSub startButton_Click(sender AsObject, 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."EndSubPrivateAsyncFunction 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(OfString) = SetUpURLList()
' Create a query.Dim downloadTasksQuery As IEnumerable(Of Task(OfInteger)) =
From url In urlList Select ProcessURLAsync(url, client)
' Use ToArray to execute the query and start the download tasks.Dim downloadTasks As Task(OfInteger)() = downloadTasksQuery.ToArray()
' You can do other work here before awaiting.' Await the completion of all the running tasks.Dim lengths AsInteger() = 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 whenAllTaskDim total = lengths.Sum()
''<snippet7>'Dim total = 0'For Each url In urlList' ' GetByteArrayAsync returns a task. At completion, the task' ' produces a byte array.' '<snippet31>' Dim urlContents As Byte() = Await client.GetByteArrayAsync(url)' '</snippet31>' ' 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'NextNext' Display the total count for all of the web addresses.
resultsTextBox.Text &= String.Format(vbCrLf & vbCrLf &
"Total bytes returned: {0}" & vbCrLf, total)
EndFunctionPrivateFunction SetUpURLList() As List(OfString)
Dim urls = New List(OfString) From
{
"https://www.msdn.com",
"https://msdn.microsoft.com/library/hh290136.aspx",
"https://msdn.microsoft.com/library/ee256749.aspx",
"https://msdn.microsoft.com/library/hh290138.aspx",
"https://msdn.microsoft.com/library/hh290140.aspx",
"https://msdn.microsoft.com/library/dd470362.aspx",
"https://msdn.microsoft.com/library/aa578028.aspx",
"https://msdn.microsoft.com/library/ms404677.aspx",
"https://msdn.microsoft.com/library/ff730837.aspx"
}
Return urls
EndFunctionPrivateAsyncFunction ProcessURLAsync(url AsString, client As HttpClient) As Task(OfInteger)
Dim byteArray = Await client.GetByteArrayAsync(url)
DisplayResults(url, byteArray)
Return byteArray.Length
EndFunctionPrivateSub DisplayResults(url AsString, content AsByte())
' 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 "https://".Dim displayURL = url.Replace("https://", "")
resultsTextBox.Text &= String.Format(vbCrLf & "{0,-58} {1,8}", displayURL, bytes)
EndSubEndClass
استخدام خدمة ويب REST باستخدام HttpClient وتنفيذ عمليات CRUD الأساسية. ستكتشف متى يكون جهازك متصلا بالإنترنت لتوفير تجربة مستخدم جيدة والاستفادة من مكدسات الشبكات الأصلية للحصول على أعلى أداء.