다음을 통해 공유


방법: Async 및 Await를 사용하여 병렬로 여러 웹 요청 만들기(Visual Basic)

비동기 메서드에서는 작업이 만들어지면 시작됩니다. Await 연산자는 작업이 완료될 때까지 처리를 계속할 수 없는 메서드의 지점에서 작업에 적용됩니다. 다음 예제와 같이 작업이 만들어지는 즉시 대기하는 경우가 많습니다.

Dim result = Await someWebAccessMethodAsync(url)

그러나 작업 완료에 따라 달라지지 않는 다른 작업이 프로그램에 있는 경우 작업 만들기를 작업 대기와 분리할 수 있습니다.

' The following line creates and starts the task.
Dim myTask = someWebAccessMethodAsync(url)

' While the task is running, you can do other work that does not depend
' on the results of the task.
' . . . . .

' The application of Await suspends the rest of this method until the task is
' complete.
Dim result = Await myTask

작업을 시작하고 기다리는 동안 다른 작업을 시작할 수 있습니다. 추가 작업은 암시적으로 병렬로 실행되지만 추가 스레드는 만들어지지 않습니다.

다음 프로그램은 세 개의 비동기 웹 다운로드를 시작한 다음 호출 순서대로 대기합니다. 프로그램을 실행할 때 태스크가 생성되고 대기된 순서대로 항상 완료되지는 않습니다. 생성될 때 실행되기 시작하고 메서드가 await 식에 도달하기 전에 하나 이상의 작업이 완료될 수 있습니다.

비고

이 프로젝트를 완료하려면 컴퓨터에 Visual Studio 2012 이상과 .NET Framework 4.5 이상이 설치되어 있어야 합니다.

동시에 여러 작업을 시작하는 또 다른 예제는 방법: Task.WhenAll을 사용하여 비동기 연습 확장(Visual Basic)을 참조하세요.

개발자 코드 샘플에서 이 예제의 코드를 다운로드할 수 있습니다.

프로젝트를 설정하려면

  1. WPF 애플리케이션을 설정하려면 다음 단계를 완료합니다. 이 단계에 대한 자세한 지침은 Async 및 Await를 사용하여 웹에 액세스하는 방법 (Visual Basic)에서 확인할 수 있습니다.

    • 텍스트 상자와 단추가 포함된 WPF 애플리케이션을 만듭니다. 단추 startButton이름을 지정하고 텍스트 상자의 이름을 지정합니다 resultsTextBox.

    • 에 대한 참조를 추가합니다 System.Net.Http.

    • MainWindow.xaml.vb 파일에서 Imports 문을 System.Net.Http에 추가합니다.

코드를 추가하려면

  1. 디자인 창의 MainWindow.xaml에서 단추를 두 번 클릭하여 MainWindow.xaml.vb에서 이벤트 처리기를 startButton_Click 만듭니다.

  2. 다음 코드를 복사하여 MainWindow.xaml.vb 본문 startButton_Click 에 붙여넣습니다.

    resultsTextBox.Clear()
    Await CreateMultipleTasksAsync()
    resultsTextBox.Text &= vbCrLf & "Control returned to button1_Click."
    

    이 코드는 애플리케이션을 구동하는 비동기 메서드 CreateMultipleTasksAsync를 호출합니다.

  3. 프로젝트에 다음 지원 메서드를 추가합니다.

    • ProcessURLAsync 에서는 메서드를 HttpClient 사용하여 웹 사이트의 콘텐츠를 바이트 배열로 다운로드합니다. 그런 다음 지원 ProcessURLAsync 메서드는 배열의 길이를 표시하고 반환합니다.

    • DisplayResults 는 각 URL에 대한 바이트 배열의 바이트 수를 표시합니다. 이 디스플레이는 각 작업 다운로드가 완료된 시기를 표시합니다.

    다음 메서드를 복사하여 MainWindow.xaml.vb 이벤트 처리기 다음에 startButton_Click 붙여넣습니다.

    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 "https://".
        Dim displayURL = url.Replace("https://", "")
        resultsTextBox.Text &= String.Format(vbCrLf & "{0,-58} {1,8}", displayURL, bytes)
    End Sub
    
  4. 마지막으로 다음 단계를 수행하는 메서드 CreateMultipleTasksAsync를 정의합니다.

    • 메서드에서 HttpClient 개체를 선언해야 하며, GetByteArrayAsync의 메서드 ProcessURLAsync에 접근해야 합니다.

    • 이 메서드는 정수인 형식 Task<TResult>TResult 의 세 가지 작업을 만들고 시작합니다. 각 작업이 완료되면 DisplayResults 작업의 URL과 다운로드한 콘텐츠의 길이를 표시합니다. 작업이 비동기적으로 실행되므로 결과가 표시되는 순서는 선언된 순서와 다를 수 있습니다.

    • 메서드는 각 작업의 완료를 기다립니다. 각 Await 연산자는 대기 중인 작업이 완료될 때까지 실행을 CreateMultipleTasksAsync 일시 중단합니다. 연산자는 각 완료된 작업에서 ProcessURLAsync 호출의 반환 값을 검색합니다.

    • 작업이 완료되고 정수 값이 검색되면 메서드는 웹 사이트의 길이를 합산하고 결과를 표시합니다.

    다음 메서드를 복사하여 솔루션에 붙여넣습니다.

    Private Async Function CreateMultipleTasksAsync() 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}
    
        ' Create and start the tasks. As each task finishes, DisplayResults
        ' displays its length.
        Dim download1 As Task(Of Integer) =
            ProcessURLAsync("https://msdn.microsoft.com", client)
        Dim download2 As Task(Of Integer) =
            ProcessURLAsync("https://msdn.microsoft.com/library/hh156528(VS.110).aspx", client)
        Dim download3 As Task(Of Integer) =
            ProcessURLAsync("https://msdn.microsoft.com/library/67w7t67f.aspx", client)
    
        ' Await each task.
        Dim length1 As Integer = Await download1
        Dim length2 As Integer = Await download2
        Dim length3 As Integer = Await download3
    
        Dim total As Integer = length1 + length2 + length3
    
        ' Display the total count for all of the websites.
        resultsTextBox.Text &= String.Format(vbCrLf & vbCrLf &
                                             "Total bytes returned:  {0}" & vbCrLf, total)
    End Function
    
  5. F5 키를 선택하여 프로그램을 실행한 다음 시작 단추를 선택합니다.

    프로그램을 여러 번 실행하여 세 작업이 항상 동일한 순서로 완료되는 것은 아니며 완료되는 순서가 반드시 생성되고 대기된 순서가 아닌지 확인합니다.

예시

다음 코드에는 전체 예제가 포함되어 있습니다.

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

Class MainWindow

    Async Sub startButton_Click(sender As Object, e As RoutedEventArgs) Handles startButton.Click
        resultsTextBox.Clear()
        Await CreateMultipleTasksAsync()
        resultsTextBox.Text &= vbCrLf & "Control returned to button1_Click."
    End Sub

    Private Async Function CreateMultipleTasksAsync() 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}

        ' Create and start the tasks. As each task finishes, DisplayResults
        ' displays its length.
        Dim download1 As Task(Of Integer) =
            ProcessURLAsync("https://msdn.microsoft.com", client)
        Dim download2 As Task(Of Integer) =
            ProcessURLAsync("https://msdn.microsoft.com/library/hh156528(VS.110).aspx", client)
        Dim download3 As Task(Of Integer) =
            ProcessURLAsync("https://msdn.microsoft.com/library/67w7t67f.aspx", client)

        ' Await each task.
        Dim length1 As Integer = Await download1
        Dim length2 As Integer = Await download2
        Dim length3 As Integer = Await download3

        Dim total As Integer = length1 + length2 + length3

        ' Display the total count for all of the websites.
        resultsTextBox.Text &= String.Format(vbCrLf & vbCrLf &
                                             "Total bytes returned:  {0}" & vbCrLf, total)
    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 "https://".
        Dim displayURL = url.Replace("https://", "")
        resultsTextBox.Text &= String.Format(vbCrLf & "{0,-58} {1,8}", displayURL, bytes)
    End Sub
End Class

참고하십시오