-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
Copy pathMainWindow.xaml.vb
85 lines (71 loc) · 3.19 KB
/
MainWindow.xaml.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
Imports System.Net.Http
Public Class MainWindow
Private ReadOnly _client As HttpClient = New HttpClient With {
.MaxResponseContentBufferSize = 1_000_000
}
Private ReadOnly _urlList As IEnumerable(Of String) =
New String() _
{
"https://docs.microsoft.com",
"https://docs.microsoft.com/azure",
"https://docs.microsoft.com/powershell",
"https://docs.microsoft.com/dotnet",
"https://docs.microsoft.com/aspnet/core",
"https://docs.microsoft.com/windows",
"https://docs.microsoft.com/office",
"https://docs.microsoft.com/enterprise-mobility-security",
"https://docs.microsoft.com/visualstudio",
"https://docs.microsoft.com/microsoft-365",
"https://docs.microsoft.com/sql",
"https://docs.microsoft.com/dynamics365",
"https://docs.microsoft.com/surface",
"https://docs.microsoft.com/xamarin",
"https://docs.microsoft.com/azure/devops",
"https://docs.microsoft.com/system-center",
"https://docs.microsoft.com/graph",
"https://docs.microsoft.com/education",
"https://docs.microsoft.com/gaming"
}
Private Sub OnStartButtonClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
_startButton.IsEnabled = False
_resultsTextBox.Clear()
Task.Run(Function() StartSumPageSizesAsync())
End Sub
Private Async Function StartSumPageSizesAsync() As Task
Await SumPageSizesAsync()
Await Dispatcher.BeginInvoke(
Sub()
_resultsTextBox.Text += $"{vbCrLf}Control returned to {NameOf(OnStartButtonClick)}."
_startButton.IsEnabled = True
End Sub)
End Function
Private Async Function SumPageSizesAsync() As Task
Dim stopwatch As Stopwatch = Stopwatch.StartNew()
Dim downloadTasksQuery As IEnumerable(Of Task(Of Integer)) =
From url In _urlList
Select ProcessUrlAsync(url, _client)
Dim downloadTasks As Task(Of Integer)() = downloadTasksQuery.ToArray()
Dim lengths As Integer() = Await Task.WhenAll(downloadTasks)
Dim total As Integer = lengths.Sum()
Await Dispatcher.BeginInvoke(
Sub()
stopwatch.[Stop]()
_resultsTextBox.Text += $"{vbCrLf}Total bytes returned: {total:#,#}"
_resultsTextBox.Text += $"{vbCrLf}Elapsed time: {stopwatch.Elapsed}{vbCrLf}"
End Sub)
End Function
Private Async Function ProcessUrlAsync(ByVal url As String, ByVal client As HttpClient) As Task(Of Integer)
Dim content As Byte() = Await client.GetByteArrayAsync(url)
Await DisplayResultsAsync(url, content)
Return content.Length
End Function
Private Function DisplayResultsAsync(ByVal url As String, ByVal content As Byte()) As Task
Return Dispatcher.BeginInvoke(
Sub()
_resultsTextBox.Text += $"{url,-60} {content.Length,10:#,#}{vbCrLf}"
End Sub).Task
End Function
Protected Overrides Sub OnClosed(e As EventArgs)
_client.Dispose()
End Sub
End Class