이 호출이 대기되지 않으므로 호출이 완료되기 전에 현재 메서드의 실행이 계속됩니다. 호출 결과에 연산자를 적용하는 Await 것이 좋습니다.
현재 메서드는 A 또는 a TaskTask<TResult> 를 반환하고 결과에 Await 연산자를 적용하지 않는 비동기 메서드를 호출합니다. 비동기 메서드 호출은 비동기 작업을 시작합니다. 그러나 연산자가 적용되지 않으므로 Await 작업이 완료되는 것을 기다리지 않고 프로그램이 계속됩니다. 대부분의 경우 해당 동작은 예상되지 않습니다. 일반적으로 호출 메서드의 다른 측면은 호출 결과에 따라 달라지거나 최소한 호출이 포함된 메서드에서 반환하기 전에 호출된 메서드가 완료되어야 합니다.
마찬가지로 중요한 문제는 호출된 비동기 메서드에서 발생하는 예외에서 발생하는 문제입니다. 반환되거나 반환 TaskTask<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를 선택하는 경우 호출자()와 호출자의 호출자CallingMethodAsync(CalledMethodAsync)가 모두 완료된 후에는 언워지지 않은 비동기 메서드(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.
예시
다음 WPF(Windows Presentation Foundation) 애플리케이션에는 이전 예제의 메서드가 포함되어 있습니다. 다음 단계에서는 애플리케이션을 설정합니다.
WPF 애플리케이션을 만들고 이름을 지정합니다
AsyncWarning.Visual Studio Code 편집기에서 MainWindow.xaml 탭을 선택합니다.
탭이 표시되지 않으면 솔루션 탐색기에서 MainWindow.xaml의 바로 가기 메뉴를 열고 코드 보기를 선택합니다.
MainWindow.xaml의 XAML 뷰에 있는 코드를 다음 코드로 바꿉니다.
<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://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를 사용하여 웹에 액세스합니다.
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.F5 키를 선택하여 프로그램을 실행한 다음 시작 단추를 선택합니다.
예상 출력은 코드의 끝에 표시됩니다.
참고하십시오
.NET