Ponieważ to wywołanie nie jest oczekiwane, wykonywanie bieżącej metody będzie kontynuowane do czasu ukończenia wywołania

Komunikat o błędzie

Ponieważ to wywołanie nie jest oczekiwane, wykonywanie bieżącej metody będzie kontynuowane do czasu ukończenia wywołania.Rozważyć zastosowanie operatora 'Await' w wyniku wywołania.

Bieżącej metody wywołania metody asynchronicznej, która zwraca Task lub Task i nie stosuje się Await operatora do wyniku.Wywołanie metody asynchronicznej uruchamia zadanie asynchroniczne.Jednakże ponieważ nie Await zastosowania operatora program nadal nie czekając na zakończenie zadania.W większości przypadków to zachowanie nie jest oczekiwany.Zazwyczaj inne aspekty metody wywołującej zależą od wyników wywołania, lub minimalny, oczekuje na zakończenie powrót z metody, która zawiera wywołanie metody o nazwie.

Równie ważnym zagadnieniem jest, co się dzieje z wyjątkami, które są wywoływane w metody asynchronicznej o nazwie.Wyjątek, który jest uruchamiany w metodzie, która zwraca Task lub Task jest przechowywany w zwracane zadania.Jeśli nie oczekują na zadania lub jawnie Sprawdź wyjątki, wyjątek zostaną utracone.Jeśli oczekują na zadania jest rethrown jej wyjątek.

Najlepiej zawsze należy poczekać wywołanie.

Domyślnie ten komunikat jest ostrzeżenie.Aby uzyskać więcej informacji na temat ukrycie ostrzeżeń lub leczenia ostrzeżenia jako błędy, zobacz Konfigurowanie ostrzeżeń w Visual Basic:.

Identyfikator błędu: BC42358

Aby adres to ostrzeżenie

  • Należy rozważyć pomijanie ostrzeżenia, tylko jeśli wiadomo nie chcesz czekać na zakończenie wywołanie asynchroniczne i że metodę o nazwie nie będzie podnieść wyjątkami.W takim przypadku można pominąć wyświetlanie ostrzeżenia przez przypisywanie zadań wynik wywołania do zmiennej.

    Poniższy przykład pokazuje sposób spowodować ostrzeżenie, jak wyłączyć go i jak oczekują na wywołania.

        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
    

    Na przykład po wybraniu wywołania # 1 lub wywołania # 2, metody asynchronicznej unawaited (CalledMethodAsync) zakończy się po obu jego rozmówcy (CallingMethodAsync) i wywołujący (StartButton_Click) są kompletne.Ostatni wiersz następujący pokazuje po zakończeniu metody o nazwie.Wejścia do i wyjścia z obsługi zdarzeń, który wywołuje CallingMethodAsync w pełny przykład są oznaczone w danych wyjściowych.

    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.
    

Przykład

Następująca aplikacja Windows Presentation Foundation (WPF) zawiera metody z poprzedniego przykładu.Następujące kroki Konfigurowanie aplikacji.

  1. Tworzenie aplikacji WPF i nadaj mu nazwę AsyncWarning.

  2. Wybierz w Visual Studio Edytor kodu, MainWindow.xaml kartę.

    Jeśli karta należy otworzyć menu skrótów dla MainWindow.xaml w Solution Explorer, a następnie wybierz polecenie Widok Kod.

  3. Zastąp kod w XAML MainWindow.xaml widok następującym kodem.

    <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>
    

    Proste okno, zawierający przycisk i pole tekstowe pojawia się w Projekt widoku MainWindow.xaml.

    Aby uzyskać więcej informacji o narzędziu Designer XAML zobacz Creating a UI by using XAML Designer.Informacje o sposobie tworzenia własnego Interfejsu prostego, zobacz "Aby utworzyć aplikacji WPF" i "do projektowania prostych MainWindow WPF" sekcje Wskazówki: uzyskiwanie dostępu do sieci za pomocą Async i Await (C# i Visual Basic).

  4. Zastąp kod w MainWindow.xaml.vb następujący kod.

    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. Wybierz klawisz F5, aby uruchomić program, a następnie wybierz Start przycisku.

    Przewidywanej produkcji pojawia się na końcu kodu.

Zobacz też

Informacje

Await — Operator (Visual Basic)

Koncepcje

Programowanie asynchroniczne z Async i Await (C# i Visual Basic)