다음을 통해 공유


이 호출이 대기되지 않으므로 호출이 완료되기 전에 현재 메서드가 계속 실행됩니다.

오류 메시지

이 호출이 대기되지 않으므로 호출이 완료되기 전에 현재 메서드가 계속 실행됩니다.'Await' 연산자 호출의 결과를 적용 하는 것이 좋습니다.

현재 메서드가 반환 하는 비동기 메서드를 호출 하는 Task 또는 Task<TResult> 및 적용 되지 않습니다는 Await 연산자 결과.비동기 작업 비동기 메서드 호출을 시작합니다.그러나 때문에 없음 Await 연산자 적용 프로그램 작업이 완료를 기다리지 않고 계속 됩니다.대부분의 경우 해당 동작이 예상 되지 않습니다.일반적으로 다른 측면을 호출 하는 메서드 호출의 결과에 따라 또는 적어도 호출된 된 메서드 호출을 포함 하는 메서드에서 반환 하기 전에 완료 예정입니다.

동일 하 게 중요 한 문제는 어떻게 호출된 비동기 메서드에서 발생 하는 예외입니다.반환 메서드에서 발생 하는 예외는 Task 또는 Task<TResult> 에서 반환 된 작업을 저장 합니다.기다립니다 작업 또는 예외를 명시적으로 확인 하지 않으면 예외가 손실 됩니다.작업을 기다립니다 경우 해당 예외를 다시 throw 되.

가장 좋은 방법은 항상 호출을 기다립니다 해야 합니다.

기본적으로 이 메시지는 경고입니다.경고를 숨기거나 경고를 오류로 처리하는 방법은 Visual Basic에서 경고 구성을 참조하십시오.

오류 ID: BC42358

이 경고를 처리하려면

  • 만 하면 비동기 호출 완료 대기 하지 않이 호출된 된 메서드가 예외를 발생 하지 않습니다 확인 되는 경우 경고를 억제 하는 것이 좋습니다.이런 경우 작업 결과를 호출 하는 변수를 지정 하 여 경고를 억제할 수 있습니다.

    다음 예제에서는 경고가 발생 하는 방법을 억제 하는 방법 및 호출을 기다립니다 하는 방법을 보여 줍니다.

        Async Function CallingMethodAsync() As Task
    
            ResultsTextBox.Text &= vbCrLf & "  Entering calling method."
    
            ' Variable delay is used to slow down the called method so that you
            ' can distinguish between awaiting and not awaiting in the program's output. 
            ' You can adjust the value to produce the output that this topic shows 
            ' after the code.
            Dim delay = 5000
    
            ' Call #1.
            ' Call an async method. Because you don't await it, its completion isn't 
            ' coordinated with the current method, CallingMethodAsync.
            ' The following line causes the warning.
            CalledMethodAsync(delay)
    
            ' Call #2.
            ' To suppress the warning without awaiting, you can assign the 
            ' returned task to a variable. The assignment doesn't change how
            ' the program runs. However, the recommended practice is always to
            ' await a call to an async method.
            ' Replace Call #1 with the following line.
            'Task delayTask = CalledMethodAsync(delay)
    
            ' Call #3
            ' To contrast with an awaited call, replace the unawaited call 
            ' (Call #1 or Call #2) with the following awaited call. The best 
            ' practice is to await the call.
    
            'Await CalledMethodAsync(delay)
    
            ' If the call to CalledMethodAsync isn't awaited, CallingMethodAsync
            ' continues to run and, in this example, finishes its work and returns
            ' to its caller.
            ResultsTextBox.Text &= vbCrLf & "  Returning from calling method."
        End Function
    
        Async Function CalledMethodAsync(howLong As Integer) As Task
    
            ResultsTextBox.Text &= vbCrLf & "    Entering called method, starting and awaiting Task.Delay."
            ' Slow the process down a little so you can distinguish between awaiting
            ' and not awaiting. Adjust the value for howLong if necessary.
            Await Task.Delay(howLong)
            ResultsTextBox.Text &= vbCrLf & "    Task.Delay is finished--returning from called method."
        End Function
    

    예제에서는 호출 하는 # 1 또는 # 2, unawaited 비동기 메서드를 호출 하면 (CalledMethodAsync) 완료 후 모두 해당 호출자 (CallingMethodAsync) 및 호출자의 호출자 (StartButton_Click) 완료 됩니다.다음 출력에서 마지막 줄에 메서드 호출된을 완료 하면 표시 됩니다.에 시작과 종료를 호출 하는 이벤트 처리기에서 CallingMethodAsync 에서 전체 예제 출력에 표시 됩니다.

    Entering the Click event handler.
      Entering calling method.
        Entering called method, starting and awaiting Task.Delay.
      Returning from calling method.
    Exiting the Click event handler.
        Task.Delay is finished--returning from called method.
    

예제

다음 Windows Presentation Foundation (WPF) 응용 프로그램에서는 이전 예제의 메서드를 포함합니다.다음 단계는 응용 프로그램을 설정 합니다.

  1. WPF 응용 프로그램을 만들고 이름을 AsyncWarning.

  2. Visual Studio 코드 편집기에서 선택 된 MainWindow.xaml 탭.

    Mainwindow.xaml에 대 한 바로 가기 메뉴는 탭이 표시 되지 않으면 열 솔루션 탐색기, 다음을 선택 하 고 코드 보기.

  3. 코드를 대체는 XAML Mainwindow.xaml의 보기는 다음 코드를 사용 합니다.

    <Window x:Class="MainWindow"
            xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Button x:Name="StartButton" Content="Start" HorizontalAlignment="Left" Margin="214,28,0,0" VerticalAlignment="Top" Width="75" HorizontalContentAlignment="Center" FontWeight="Bold" FontFamily="Aharoni" Click="StartButton_Click" />
            <TextBox x:Name="ResultsTextBox" Margin="0,80,0,0" TextWrapping="Wrap" FontFamily="Lucida Console"/>
        </Grid>
    </Window>
    

    단추와 텍스트 상자 포함 된 간단한 창이 표시 되는 디자인 MainWindow.xaml 보기.

    XAML 디자이너에 대 한 자세한 내용은 XAML 디자이너를 사용하여 UI 만들기.간단한 UI를 작성 하는 방법에 대 한 자세한 내용은 "WPF 응용 프로그램 만들기" 및 "간단한 WPF MainWindow 디자인" 섹션의 연습: Async 및 Await를 사용하여 웹에 액세스(C# 및 Visual Basic).

  4. Mainwindow.xaml.vb의 코드를 다음 코드로 바꿉니다.

    Class MainWindow 
    
        Private Async Sub StartButton_Click(sender As Object, e As RoutedEventArgs)
    
            ResultsTextBox.Text &= vbCrLf & "Entering the Click event handler."
            Await CallingMethodAsync()
            ResultsTextBox.Text &= vbCrLf & "Exiting the Click event handler."
        End Sub
    
    
        Async Function CallingMethodAsync() As Task
    
            ResultsTextBox.Text &= vbCrLf & "  Entering calling method."
    
            ' Variable delay is used to slow down the called method so that you
            ' can distinguish between awaiting and not awaiting in the program's output. 
            ' You can adjust the value to produce the output that this topic shows 
            ' after the code.
            Dim delay = 5000
    
            ' Call #1.
            ' Call an async method. Because you don't await it, its completion isn't 
            ' coordinated with the current method, CallingMethodAsync.
            ' The following line causes the warning.
            CalledMethodAsync(delay)
    
            ' Call #2.
            ' To suppress the warning without awaiting, you can assign the 
            ' returned task to a variable. The assignment doesn't change how
            ' the program runs. However, the recommended practice is always to
            ' await a call to an async method.
    
            ' Replace Call #1 with the following line.
            'Task delayTask = CalledMethodAsync(delay)
    
            ' Call #3
            ' To contrast with an awaited call, replace the unawaited call 
            ' (Call #1 or Call #2) with the following awaited call. The best 
            ' practice is to await the call.
    
            'Await CalledMethodAsync(delay)
    
            ' If the call to CalledMethodAsync isn't awaited, CallingMethodAsync
            ' continues to run and, in this example, finishes its work and returns
            ' to its caller.
            ResultsTextBox.Text &= vbCrLf & "  Returning from calling method."
        End Function
    
        Async Function CalledMethodAsync(howLong As Integer) As Task
    
            ResultsTextBox.Text &= vbCrLf & "    Entering called method, starting and awaiting Task.Delay."
            ' Slow the process down a little so you can distinguish between awaiting
            ' and not awaiting. Adjust the value for howLong if necessary.
            Await Task.Delay(howLong)
            ResultsTextBox.Text &= vbCrLf & "    Task.Delay is finished--returning from called method."
        End Function
    
    End Class
    
    ' Output
    
    ' Entering the Click event handler.
    '   Entering calling method.
    '     Entering called method, starting and awaiting Task.Delay.
    '   Returning from calling method.
    ' Exiting the Click event handler.
    '     Task.Delay is finished--returning from called method.
    
    
    ' Output
    
    ' Entering the Click event handler.
    '   Entering calling method.
    '     Entering called method, starting and awaiting Task.Delay.
    '     Task.Delay is finished--returning from called method.
    '   Returning from calling method.
    ' Exiting the Click event handler.
    
  5. 프로그램을 실행 한 다음 선택 합니다 F5 키를 선택 하 여 시작 단추.

    예상된 출력 코드의 끝에 나타납니다.

참고 항목

참조

Await 연산자(Visual Basic)

개념

Async 및 Await를 사용한 비동기 프로그래밍(C# 및 Visual Basic)